meteor-ios icon indicating copy to clipboard operation
meteor-ios copied to clipboard

Detecting currentUser

Open trevorbroad opened this issue 9 years ago • 1 comments

Hey again, the app I'm developing requires intermittent usage--a common use-case is for the user to unlock their phone, enter some data, then lock their phone again for 5-15 minutes before unlocking the phone again and entering more data, and so-on and on. The first problem is the connection to the server doesn't persist for too long (you mention as much in your 'implementation details' section). So each time the user unlocks the phone, the app resets to the startup screen. This has caused me to develop a 'recovery' solution where on the startup screen I want to detect if a task wasn't completed, and send the user directly back to the proper screen.

The second problem I'm now encountering with this solution is that I want to perform a fetch request as soon as possible after loading the startup screen to check for the user's incomplete tasks and send them on their way, but when I check for the currentUser in viewDidLoad, it returns nil. If I check currentUser again at viewWillDissapear, I do get the user object, so it is working. What I'm looking for is a callback (delegate?) that I can use to execute code the second the currentUser object is found. I'm using most of the swift code from your demo app for the FetchedResultsDataSource and FetchedResultsTableViewController (but not FetchedResultsTableViewDataSource as I'm not loading the data into a table). I'm checking the following in FetchedResultsTableViewController:

var currentUser: User? {
    if let userID = Meteor.userID {
        let userObjectID = Meteor.objectIDForDocumentKey(METDocumentKey(collectionName: "users", documentID: userID))
        return managedObjectContext.existingObjectWithID(userObjectID, error: nil) as? User
    }
    return nil;
}

I guess I have 2 questions. 1: I thought of the recovery solution which will work ok, and I'll likely need the feature anyway so I'm going ahead with it now, but is there a better solution to this problem that you know of? 2: How do you recommend executing code on app startup as soon as the user is detected?

Thanks in advance for your help

trevorbroad avatar Apr 23 '15 05:04 trevorbroad

You can use the METDDPClientDidChangeAccountNotification notification. Just show a splash or login view controller until that notification is received and the currentUser variable you referenced above returns a User.

For example (In my AppDelegate, using ReactiveCocoa):

    func setupLocalNotifications() {
        let notificationCenter = NSNotificationCenter.defaultCenter()

        notificationCenter
            .rac_addObserverForName(METDDPClientDidChangeAccountNotification, object: Meteor)
            .deliverOn(RACScheduler.mainThreadScheduler())
            .subscribeNext { _ in
                if let currentUser = Meteor.currentUser {
                    self.setHome()
                } else {
                    self.setOnboarding()
                }
            }
    }

Where I added currentUser to my METCoreDataDDPClient with an extension.

justinmakaila avatar Jun 05 '15 19:06 justinmakaila