macosparse-platformpfuserosx-elcapitanparse-osx-sdk

PFUser currentUser nil after app restart on OS X


I've downloaded the latest Parse SDK for OS X, and I'm trying to retain my login after app restarts (obviously). I've used Parse before and I haven't faced this problem, neither on iOS nor OS X.

On my app start:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    [Parse setApplicationId:@"XXX" clientKey:@"XXX"];
}

In my first view controller:

-(void)viewDidAppear{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(![PFUser currentUser]){
            [self performSegueWithIdentifier:@"login" sender:nil];
        }else{
            ...
        }
    });
}

My login succeeds, and at that point [PFUser currentUser] is valid. Then, I close the app (tried both killing and gracefully quitting). When I open it again, [PFUser currentUser] is nil. I've tried this many times, it yields the same results. Why?


Solution

  • After struggling for a long while, I've found the solution. I need to dispatch_async the user checking block and it starts working. So instead of:

    dispatch_once(&onceToken, ^{
        if(![PFUser currentUser]){
            [self performSegueWithIdentifier:@"login" sender:nil];
        }else{
            ...
        }
    });
    

    I did:

    dispatch_once(&onceToken, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if(![PFUser currentUser]){
                [self performSegueWithIdentifier:@"login" sender:nil];
            }else{
                ...
            }
        });   
    });
    

    And it started working. Interesting to see that something is still not initialized on viewDidAppear synchronously on main queue (yes, it IS the main queue, double checked that), but is initialized somewhere after posting to the same queue asynchronously. Parse definitely needs more quality control of their SDK.