Question

I cannot fixe a little problem I have with ths following method :

+ (User *) createUserForLoginWithFacebookToken: (NSString *) facebookToken andExpirationToken: (NSDate *) expirationToken{
    //Init current user before having collected data from API after authentication

    NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    managedObjectContext.parentContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"User" inManagedObjectContext:managedObjectContext.parentContext];
    User * result = [[User alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:managedObjectContext];
    result.email=@"defaultValue@test.com";
    result.gender=[NSNumber numberWithInt:-1];
    if (facebookToken != nil && expirationToken != nil && ![facebookToken isEqualToString:@""] && [expirationToken compare:[NSDate date]]==NSOrderedDescending) {
        result.facebookToken=facebookToken;
        result.facebookExpiration=expirationToken;
    }

    return result;
}

before the return, the result variable is created and some attributes are set. But when I take this returned object with this method:

currentUser=[User createUserForLoginWithFacebookToken:facebookToken andExpirationToken:facebookExpiration];

The currentUser variable exists but all attributes are nil.

I'm a beginner on objective-c, I'm pretty sure the solution is obvious but I cannot fix it right now.

Thank you in advance for your help.

Was it helpful?

Solution

Oh, I see. You're creating a MOC just for this method and never merging it with the context you're using outside the scope of the method, so your changes after insertion go unsaved. I bet a number of people just skimmed right passed that because the whole MOC entityname/insert junk is always so verbose.

With methods of my own similar to your createUserForLoginWithFacebookToken:, I pass in the MOC that I'm using as an additional method parameter of NSManagedObjectContext* type. I wouldn't allocate a whole new MOC, insert one object into it, and then merge. In general, there's one MOC per thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top