1

I am using stackmob for backend service. I create a new user using these codes:

User *newUser = [[User alloc] initIntoManagedObjectContext:self.managedObjectContext];
[newUser setValue:info.username forKey:[newUser primaryKeyField]];
[newUser setValue:info.email forKey:@"email"];
[newUser setPassword:info.password];
NSError *error = nil;
if ([self.managedObjectContext save:&error]) {
}

User logs in using these codes:

[self.client loginWithUsername:info.username password:info.password onSuccess:^(NSDictionary *result) {
    [userDefaults setBool:YES forKey:@"signedin"];
    NSLog(@"user signed in");
} onFailure:^(NSError *error) {
    NSLog(@"login error");
}];

Right now the Event has many users.

So, I want to [self.event addUsersObject:(User *)]. But how do i get current logging user, so I can add current user into this event.

Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
yong ho
  • 3,892
  • 9
  • 40
  • 81

2 Answers2

0

In the onSuccess block of loginWithUsername you need to query the User entity with the username you logged in with. Something like this...

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

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username=%@", info.username];
[fetchRequest setPredicate:predicate];

[self.managedObjectContext executeFetchRequest:fetchRequest onSuccess:^(NSArray *results){
    ///there should be only 1 result, but the code should really check
    NSLog(@"%@",results[0]);
    User* loggedInUser = results[0];
    [self.event addUserObject:loggedInUser];

} onFailure:^(NSError *error) {
    NSLog(@"Error fetching: %@", error);
}];
combinatorial
  • 9,132
  • 4
  • 40
  • 58
  • so, I just have to [self.event addUserObject:entity]? Where is the addUserObject part? – yong ho Jan 31 '13 at 14:23
  • you need to do that in the executeFetchRequest onSuccess handler. I updated the answer. – combinatorial Jan 31 '13 at 16:48
  • I am sorry I give the answer check marker to Sidney Maestre. His codes really helped me a lot, answered many my questions. But I appreciate your help too. – yong ho Feb 02 '13 at 12:33
0

I'm the platform evangelist for StackMob.

I just created a project where I fetched the currently logged in user and add that object to a relationship. You can download and take a look at the code yourself.

https://github.com/SidneyAllen/hollagram-ios

Look inside the JustSayinViewController.m file for the appropriate code.

Sidney Maestre
  • 383
  • 1
  • 5
  • Another question: I have two models, Event <<->> User. I use your solution to addUserObject, but the situation is [self.event addUsersObject:currentLoggedInUser]; is not working. it's blocking the ui, i can't click anything. After I change the relationship local, do i have to change or add relationship at dashboard.stackmob.com? – yong ho Feb 02 '13 at 13:33
  • I would check the dashboard and confirm the relationship is setup correctly for your schemas. – Sidney Maestre Feb 04 '13 at 19:43