NSUbiquitousKeyValueStore taking too long for initial sync - ios

I want to use NSUbiquitousKeyValueStore to store in iCloud some simple key-value pairs for a game I am making. I was under the impression that if the user deleted and then reinstalled the game, their progress would be restored when the app launched.
This appears not to be the case. From the testing I have done, the key-value pairs take a long time to get downloaded from iCloud upon the first launch of the app. After that, the data seems to get uploaded and downloaded almost instantly. This causes issues for my app because when it is reinstalled, it does not immediately have the users previous data and creates a new set of data, negating the point of using NSUbiquitousKeyValueStore.
Is there a way to ensure that information from NSUbiquitousKeyValueStore is available as soon as possible after the app is first launched, and if not what other iCloud APIs could I use?

To ensure that information from NSUbiquitousKeyValueStore is available as soon as possible after the app is first launched. You need to do two steps.
Step 1:- Register for the
NSUbiquitousKeyValueStoreDidChangeExternallyNotification
notification during app launch.
Step 2:- Call the Instance Method
NSUbiquitousKeyValueStore.default.synchronize() The recommended
time to call this method is upon app launch, or upon returning to the
foreground.
For Example:-
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ubiquitousKeyValueStoreDidChange), name: NSUbiquitousKeyValueStore.didChangeExternallyNotification, object: NSUbiquitousKeyValueStore.default)
NSUbiquitousKeyValueStore.default.synchronize()
// referesh and retrieve keys
}
#objc func ubiquitousKeyValueStoreDidChange(notification:Notification) {
// Get the reason for keys changed
let changeReason = notification.userInfo![NSUbiquitousKeyValueStoreChangeReasonKey] as! Int
// get keys changed.
let changeKeys = notification.userInfo![NSUbiquitousKeyValueStoreChangedKeysKey] as! [String]
switch changeReason {
case NSUbiquitousKeyValueStoreInitialSyncChange, NSUbiquitousKeyValueStoreServerChange, NSUbiquitousKeyValueStoreAccountChange:
// referesh and retrieve keys
case NSUbiquitousKeyValueStoreQuotaViolationChange:
// Reduce Data Stored
}
}
NB: All types of iCloud storage, for example, NSUbiquitousKeyValueStore offer only eventual consistency and thus does not support instant multiple device access.

Related

NSPersistentStoreCoordinatorStoresWillChangeNotification being called even though no internet connect

I'm trying to setup iCloud on my app by following Enabling iCloud Support from Apple. I have added an iCloud-Enabled Persistent Store to Core Data, and retrieved the required log messages.
I then removed my app and reinstalled it. My NSPersistentStoreCoordinatorStoresDidChangeNotification is called and so now I am up to the third part which is iCloud Performs One Time Setup and here is my code:
func subscribeToPersistentStoreCoordinatorStoresNotifications() {
// Subscribe for first time setup of iCloud
NSNotificationCenter.defaultCenter().addObserver(self, selector: "enableUI", name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: sharedContext.persistentStoreCoordinator)
NSNotificationCenter.defaultCenter().addObserverForName(NSPersistentStoreCoordinatorStoresWillChangeNotification, object: sharedContext.persistentStoreCoordinator, queue: NSOperationQueue.mainQueue()) { notification in
// First reset the managed object context
self.sharedContext.performBlock() {
print("=== RESETTING CONTEXT ===")
self.sharedContext.reset()
}
// Then disable UI
dispatch_async(dispatch_get_main_queue()) {
print("=== DISABLING UI===")
self.disableUI()
}
}
}
I have just followed the template that Apple gave and this function is called in my viewWillAppear in the first View Controller that users see.
So then I followed Apple's setup and testing routines:
Setup: On your iOS device, begin with airplane mode enabled. If you’re creating a Mac app, disable every network interface instead. Completely remove your app from your device as well.
Test: Run your app and create a few records. Disable airplane mode or
reenable a network interface and wait for Core Data to print “Using
local storage: 0” to the console in Xcode. Core Data invokes your
notification handlers and your records disappear. In the next section
you’ll learn how to persist in-memory changes.
Every goes well until I start testing it, even though I start my app in Airplane mode I think somehow my iPhone is still able to connect to iCloud and as a result the NSPersistentStoreCoordinatorStoresWillChangeNotification is being called.
Note: I have also tested it with data being stored in CoreData and then deleting the app when the data has been successfully uploaded to iCloud. When I reinstall the app in Airplane mode, I am still able to retrieve the stored data.
Any ideas?

WatchOS storing and sharing data

I have a Core Data app. It is like a let's say a News app. Each entry has name,id,date,publisher,detail etc. Main iOS app can have lots of News entries. I only want to show let's say first 3 news with the WatchOS app. Since getting news entries needs keyboard usage, I can't initiate transfers from the Watch side. What is the good strategy to share the data? I have thought following scenarios
Send Core Data files with WatchConnectivity transferFile
PROS: Easy
Huge amount of unnecessary data, may not have latest data if the changes aren't saved to context yet.
Whenever News is added send with WatchConnectivity before saving to CoreData.
PROS: Always same data,
CONS: Huge amount of unnecessary data, extra operations to save to new database
When the data is saved to Core Data, query last three objects and send them.
PROS: Small amount of data,
CONS: Need to convert NSManagedObject to another object first, may send same data
Could you help me to find a better way to sync iOS app with WatchOS app? Thanks.
I think the best approach would be background transfers using the application context. That has the following advantages:
You don't have to care about whether your watch app is running or not. When you add your data to the application context it gets added to to a transfer queue and whenever the watch app gets active, it receives the data.
Every time you add your three items, the old items get overwritten. So you always have only 3 items in the queue. That's ideal for a news app, where you don't want to bother your user with old news. So sending the same data several times is no problem, only the latest data "survives".
The only downside would be that you have to serialize your NSManagedObject. I don't know how complex your objects are, but if they are you could use library like HyperSync or Groot
So this is how you would then sync your phone with your app:
1. Set up the session:
if WCSession.isSupported() {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
Do this on both places: In your main app and also in the watch extension. If you are only sending data from your main app to the watch you do not need to set the delegate on the main app side.
2. Implement the delegate method:
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
// deserialize the received data,
// store it in CoreData on your watch
// and update the UI
}
3. Send the data:
let dataDict = latestThreeNewsObjects.serializeToDictionary() // However you achieve this ;-)
do {
try WCSession.defaultSession().updateApplicationContext(dataDict as! [String : AnyObject])
} catch {
print("Cannot send data to watch: \(error)")
}
So then, every time you add new news items to your main app CoreData, fetch the latest three NSManagedObjects, serialize them into a dictionary and update your application context. This way the watch always has the latest 3 news when it gets active. When it already is active, the news get updated immediately.
One more thing: Before trying to send data to the watch, you should always check if the user has installed the app on his watch. WCSession has a property for that: watchAppInstalled. If the app is not installed, don't waste resources sending data into the abyss...

NSUbiquityIdentityDidChangeNotification doesn't work?

I'm creating a cloudkit app, and have been trying multiple ways to get the NSUbiquityIdentityDidChangeNotification, but I never am able to get this notification.
I've tried both of these code versions under the delegate didFinish and the viewDidLoad methods. And I tried calling it from another notification - UIApplicationDidBecomeActiveNotification. I also put import Foundation at top of files.
Here's the basic code I've tried:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "handleIdentityChanged:",
name: NSUbiquityIdentityDidChangeNotification,
object: nil)
// And this one I tried too from another post here on SO:
var localeChangeObserver = NSNotificationCenter.defaultCenter().addObserverForName(NSUbiquityIdentityDidChangeNotification, object: nil, queue: NSOperationQueue.mainQueue()) { _ in
println("The user’s iCloud login changed: should refresh all user data.")
}
Does anyone know how to get this notification to work for only a cloudkit app in swift? I really just want to detect the iCloud status change and then initiate fetching the userID if there's been a change.
Not that I need to access the ubiquityIdentityToken, but I was wondering why not store the token and every-time the app starts compare the current token with the one in local storage to see if it's a different account or nil? Therefore, why is getting the notification necessary?
Also, the code for getting the token only seems to work if I turn on "iCloud Documents", which I don't need. Does anyone know the implications of having that turned on for a social app that doesn't need it? And is there another way to get the token without enabling iCloud Documents?
This is the code I used to get token and placed in the delegate didFinish method, but only works if iCloud documents is turned on:
var token = NSFileManager.defaultManager().ubiquityIdentityToken
println("token is \(token!)")
On iOS, when I sign out of iCloud, my app is killed. So there seems not really to be a need to receive a NSUbiquityIdentityDidChangeNotification. Like you have said, it seems to be sufficient to compare the current token to the saved token.
On the Apple TV though, my app was not killed when I logged out of iCloud. Here I had noticed the notification was not fired, like you described. Since the app is not killed, a notification would be in order. (Did Apple forget to kill apps on Apple TV when iCloud account is changed?)
With Apple TV there is no iCloud documents container available (unless I explicitly share one from an iOS app). I found that on the dev center website, for the app identifier, iCloud was shown as "Configurable" and not "Enabled" if no document container was selected. I wonder if this has an effect on receiving notifications.
Both on the Apple TV and iOS, I can also confirm that the iCloud token is nil when not using documents (here: key-value-store only). Now that makes it difficult for Apple TV apps (because the app is not killed on iCloud account change, like on iOS) to detect account changes.
I have just noticed that my Apple TV app does received several NSUbiquitousKeyValueStoreDidChangeExternallyNotification when I log into another iCloud account, to reflect the changes. I guess this is as good as it gets. These notifications come with the NSUbiquitousKeyValueStoreChangeReasonKey key in userInfo, and a value of NSUbiquitousKeyValueStoreAccountChange indicates the account has changed.
Sorry for not being able to provide a direct solution, maybe it helped to share my experience.
To be notified in iOS when a user logs in or out of iCloud while using your app, use CKAccountChangedNotification instead of NSUbiquityIdentityChanged notification.
(Longer explanation: https://stackoverflow.com/a/38689094/54423.)

iCloud + Core Data: First import and user's feeling of loss of data

I've implemented an iPhone application that has around 50k users. Switching from iOS7 to iOS8 a lot of these users have experienced a terrible feeling when they thought that they data get lost.
I've implemented the first-import behaviour that I thought was the one suggested by Apple
1) Users launch the App
2) iCloud, automatically, starts synching data previously stored on iCloud
3) At some point user get notified that data from iCloud is ready thanks to NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted
The problem is with 3) At some point:
Users that have to sync a lot of data need minutes to get the synch completed and in the meanwhile they think that their data is lost.
I really don't know how to let my users know that they have to wait to see their data synched, because I don't know when this operation starts.
I'm thinking about a possible solution:
During the first launch of the App, asking to the user if he wants to use iCloud. If he chooses to use it, building the database with iCloud options, so I know exactly that the synch is starting here (I suppose...)
I'm really not sure about how to implement this behaviour since I've always seen Core Data settings into the AppDelegate but to achieve this behaviour I suppose I need to move all the CoreData settings in a Controller.
What do you think about this solution? how are you working around this problem in you Apps?
Your idea is right, at least it is that what we do. But leave it in the appDelegate.
Differentiate between with iCloud and without iCloud when doing the "addPersistentStoreWithType". If you do it with iCloud options, it will directly start to build the local store which is a kind of a placeholder ( I'm sure you know that, but just to make my thoughts clear). As soon as this is done, the sync starts from iCloud. So this is the starting point I understood you were looking for.
You can watch that process using the notifications by NSPersistentStoreCoordinatorStoresDidChangeNotification and inform you user accordingly triggered by that notification.
If you look at "Reacting to iCloud Events" in the docs https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW5 there is a detailed desc.
To summarize, the event you're describing is part of the account transitions process. An account transition occurs when one of the following four events is triggered:
Initial import
the iCloud account used did change
iCloud is disabled
your application's data is deleted
During this event, Core Data will post the NSPersistentStoreCoordinatorStoresWillChangeNotification and NSPersistentStoreCoordinatorStoresDidChangeNotification notifications to let you know that an account transition is happening. The transition type we're interested in is NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted.
For information, I've moved all Core Data related code to my own Manager for simplicity and use it with a singleton design pattern. While setting up the singleton, I register the Manager for all relevant notifications (NSPersistentStoreDidImportUbiquitousContentChangesNotification, NSPersistentStoreCoordinatorStoresWillChangeNotification, NSPersistentStoreCoordinatorStoresDidChangeNotification, NSPersistentStoreCoordinatorWillRemoveStoreNotification).
I store several informations in my settings (NSUserDefaults or anything) like the last iCloud state (enabled, disabled, unknown), if the initial import is done or not, etc.
What I end up doing was having a prompt (UIAlertController or anything) to get a confirmation if the user wants to use iCloud or not. I have a displayICloudDialogAndForce:completion: method to do that and only do that if my iCloud state setting is unknown or I use the force parameter.
Then, after the user input, I call a setupCoreDataWithICloud: method, the iCloud boolean parameter depending on the user choice. I would then setup my Core Data stack, on the cloud or not according to the iCloud parameter.
If I'm setting up using iCloud, I would check my settings for the value of an iCloud imported key (boolean). If the value is NO, then I'm presenting a new modal to warn the user about the incoming import that could take some time.
I've registered my manager for different notifications and specially NSPersistentStoreCoordinatorStoresDidChangeNotification. In my storeDidChange: callback, I'm checking the transition type and if it's NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted, I'm changing the content of my modal to show the user that the import was successful and removing it a few seconds later, saving in my settings that the initial import is done.
- (void)storeDidChange:(NSNotification *)notification
{
NSPersistentStoreUbiquitousTransitionType transitionType = [notification.userInfo[NSPersistentStoreUbiquitousTransitionTypeKey] integerValue];
if (transitionType == NSPersistentStoreUbiquitousTransitionTypeInitialImportCompleted) {
[settings setDefaults:#(YES) forKey:kSettingsICloudImportedKey];
[ICloudModal dismissWithSuccess];
// ...
}
// Do other relevant things...
}

Definitive Way to Save Data Between Launches.

For a project I'm working on, I would like to save a few bytes of data to the users iPhone between launches of the application. I would like to do this so I can save some state and a few important numbers when the user terminates the app. I have thought about it, and the best place to do this seems like the AppDelegate.
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate.
// Save data if appropriate. See also applicationDidEnterBackground:.
/* this is where I want to begin the process of saving application data */
}
I have heard of writing objects (only a select few allowed such as NSString, NSDictionary, NSArray) to file, but can this be kept around between launches of the app, or does the data go away when the user terminates the app?
Question:
Is there a definitive way to write data to the users iPhone that will stick around after the application quits?
The most common approach is to persist whatever data or state you need when your app enters the background. Then the data will be there if the app is killed while in the background and the app is restarted.
How you store the data really depends on what the data is. Writing the data to a file in the app's sandbox is quite common. Small bits of data can also be stored in NSUserDefaults.

Resources