0

Using StackMob as backend. I have an Event entity which has a relationship called user users (type NSSet). Right now I want to get all the events that some user's username is @"someuser". My code like this:

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

    }];
yong ho
  • 3,892
  • 9
  • 40
  • 81

2 Answers2

2

If I understand your question correctly, you have to use the following predicate:

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

It finds all events that have any user with the given user name.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • @yongho: Why does it not work? Do you get an error message or no results or wrong results? – Martin R Feb 24 '13 at 07:33
  • @yongho: I tested it myself and it worked. Perhaps I did not understand your problem correctly. Could you show the NSLog() output of `predicate` and `request`? – Martin R Feb 24 '13 at 08:14
  • predicate: ANY users.username == "testuser" request: (entity: Event; predicate: (ANY users.username == "testuser"); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) – yong ho Feb 25 '13 at 08:07
  • @yongho: Strange, I have tested it once more and it worked for me. Can you try the following: Remove the predicate from the fetch request and show the output of `NSLog(@"%@", [events valueForKeyPath:@"users.username"]);` – Martin R Feb 25 '13 at 08:33
  • "{(\n test\n)}", "{(\n)}", "{(\n testuser\n)}" – yong ho Feb 25 '13 at 08:57
  • So your first event has user "test", the second event has no user, and the third event has user "testuser"? I checked the same configuration and it worked for me. Are you sure that you get no results if you set the predicate as in my answer? – Martin R Feb 25 '13 at 09:10
  • Yes, I am pretty sure about that. – yong ho Feb 25 '13 at 09:35
  • @yongho: What happens if you remove the predicate from the fetch request, and execute `NSArray *filteredEvents = [events filteredArrayUsingPredicate:predicate];` with the predicate instead (after fetching the events)? – Martin R Feb 25 '13 at 09:38
  • Yeah, I am getting results back, loads of stuff. So, [request setPredicate:predicate]; isn't working. – yong ho Feb 25 '13 at 09:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25068/discussion-between-martin-r-and-yong-ho) – Martin R Feb 25 '13 at 09:44
  • Can you send me the working source code for this. I really need to solve this problem, Could you help me with that. Maybe I think by looking your codes, could help me solve this problem. – yong ho Mar 02 '13 at 14:38
  • @yongho: You might be interested in this: http://stackoverflow.com/questions/16110266/nspredicate-traversing-relationship-stackmob. – Martin R Apr 22 '13 at 08:06
0

Assuming you have a User entity and an Event entity, you have to pass a User to the predicate, not just the name.

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

fetchRequest.predicate = [NSPredicate predicateWithFormat:
                           @"%@ in users", loggedInUser]
Mundi
  • 79,884
  • 17
  • 117
  • 140