0

使用 StackMob 作为后端。我有一个Event实体,它的关系称为 user users(type NSSet)。现在我想获取某个用户的所有username事件@"someuser"。我的代码是这样的:

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

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

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY users.username = %@", currentlyLoggedInUser];
        NSLog(@"predicate: %@", predicate);
        [request setPredicate:predicate];
        NSLog(@"request: %@", request);
        events = [NSArray new];
        events = [self.managedOjbectContext executeFetchRequest:request error:nil];

        /* this is working...
        for (Event *e in events) {
            NSLog(@"%@",[e.users filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"username == %@", currentlyLoggedInUser]]);

        }
         */
        NSLog(@"events: %@", events);
    } onFailure:^(NSError *error) {

    }];
4

2 回答 2

2

如果我正确理解您的问题,您必须使用以下谓词:

NSString *userName = @"some user";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY users.username = %@", userName];

它查找具有给定用户名的任何用户的所有事件。

于 2013-02-22T14:48:37.303 回答
0

假设您有一个 User 实体和一个 Event 实体,您必须将一个 User 传递给谓词,而不仅仅是名称。

User *loggedInUser = // anything, e.g.
User *loggedInUser = [[allUsers filteredSetUsingPredicate:
                       [NSPredicate predicateWithFormat:
                         @"username == %@", currentlyLoggedInUser] anyObject];

fetchRequest.predicate = [NSPredicate predicateWithFormat:
                           @"%@ in users", loggedInUser]
于 2013-02-22T17:46:11.257 回答