How to get the sm_owner User object using StackMob as backend

蓝咒 提交于 2019-12-13 06:49:48

问题


I find out the User has a sm_owner field. But how do I get the user using sm_owner. My code is like this:

    [self.client getLoggedInUserOnSuccess:^(NSDictionary *result) {
        NSString *currentLogginedUser = [result valueForKey:@"sm_owner"];
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedOjbectContext];
        [request setEntity:entity];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sm_owner like user/%@", currentLogginedUser];
        [request setPredicate:predicate];
        self.user = [[self.managedOjbectContext executeFetchRequest:request error:nil] objectAtIndex:0];
    } onFailure:^(NSError *error) {

    }];

And the crash message is:reason: 'Unimplemented SQL generation for predicate (sm_owner LIKE user / "user/test")'


回答1:


I'm the platform Evangelist for StackMob.

The result from the getLoggedInUserOnSuccess method normally contains primary key "username". So the following code should work.

[self.client  getLoggedInUserOnSuccess:^(NSDictionary *result) {                
    NSString *currentLogginedUser = [result objectForKey:@"username"];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:self.managedOjbectContext];
    [request setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username == %@", currentLogginedUser];
    [request setPredicate:predicate];
    self.user = [[self.managedOjbectContext executeFetchRequest:request error:nil] objectAtIndex:0];
} onFailure:^(NSError *error) {

}];                

Alternatively, if you rather grab the sm_owner from the NSDictionary result. I would do the following.

NSString *sm_owner = [result valueForKey:@"sm_owner"];

NSArray *myArray = [sm_owner componentsSeparatedByString:@"/"];
NSString *currentLogginedUser = [myArray objectAtIndex:1];

Then perform your fetch.



来源:https://stackoverflow.com/questions/14663490/how-to-get-the-sm-owner-user-object-using-stackmob-as-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!