0

我发现 User 有一个 sm_owner 字段。但是如何让用户使用 sm_owner。我的代码是这样的:

    [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) {

    }];

并且崩溃消息是:原因:'未实现的 SQL 生成谓词(sm_owner LIKE user / "user/test")'

4

1 回答 1

1

我是 StackMob 的平台布道者。

getLoggedInUserOnSuccess 方法的结果通常包含主键“用户名”。所以下面的代码应该可以工作。

[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) {

}];                

或者,如果您宁愿从 NSDictionary 结果中获取 sm_owner 。我会做以下事情。

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

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

然后执行您的提取。

于 2013-02-04T19:37:46.477 回答