Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudKit : How to handle account changes with local persistent store?

In my application i have to maintain a local persistent store in sync with cloud kit private database. So I just wanted to know how can I handle account changes that may happen.

Confusion I have is as below: say a set of records belong to user A now if user B log's in to the same phone I can do the following of the 2 things:

  1. Ignore user and let data sync to B account too but that way A's data will get sync to B's private account too. Here the record change tag and all get a bit mess up since am saving CKRecord encoded fields to database.

  2. I can maintain a user table and link each record to the user that is logged in that way user data will get separated. So should I maintain a user field along with all records ?

How can this be best handled even apart from above 2 things.

like image 684
user3519594 Avatar asked Oct 30 '22 17:10

user3519594


1 Answers

Of course in your local persistence store you could add the userID to personalize all records. An other mechanism is to remove all local data and fetch the users data when a change is detected. If you want to keep the users data on the device you could also create separate data stores for each user.

You can detect a changed login by adding the following code in your app delegate or root view controller:

NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: nil) { _ in
     /// remove local data and fetch user data
}

You should also refresh all user related data in memory and refresh the loaded views.

like image 121
Edwin Vermeer Avatar answered Nov 13 '22 06:11

Edwin Vermeer