Open GoBananas opened 10 years ago
If you have an existing Core Data store, you can use migratePersistentStore to migrate the store to iCloud, using the iCloud options as if you were adding the store.
You'll have to take care of de-duplication, as if you migrate the same store from two different devices to iCloud, you'll get the records duplicated. How you implement de-duping depends on your data model.
This is very helpful! I also have an existing data store and I am not having much luck migrating it into iCloud - do either of you have an example on how to do this?
What I did (in a nutshell):
I marked the merged objects so the user is able to identify the migrated ones. This was important if he migrates from an existing local to a iCloud store.
I don't know if there is a better way, but this works for my App which uses a tine Core Data structure.
Hi! I'm trying to migrate data from local to iCloud using migratePersistentStore like this:
NSPersistentStore *newStore = [psc migratePersistentStore:currentStore toURL:[self icloudStoreURL] options:@{ NSPersistentStoreUbiquitousContentNameKey : @"myapp" } withType:NSSQLiteStoreType error:&error];
To update UI:
(void) storeDidChange { _fetchedResultsController = nil;
self.managedObjectContext = [AppDelegate sharedAppDelegate].persistentStack.managedObjectContext;
[self fetchedResultsController];
[self.tableView reloadData]; }
But it doesn't work for me. Is there way to get notification when migration process has finished?
Is the problem that it doesn't work at all, or that it works, but you don't know when it's finished? The problem with not getting a notification is actually a bug in iCloud. You get the PersistentStoresDidChange notification before the iCloud store has fully loaded, so when you refresh your UI, you still get the old local store. I actually demoed this problem to an Apple Engineer when I was at WWDC a couple of weeks ago and a Radar has been created.
The workaround I use is to use dispatch_after
in order to wait 5 seconds after I get the notification before updating the UI. It's a really messy solution, especially as 5s may not always be enough, but it's the only workaround I have.
Thanks for posting this - it definitely clarifies the process! My question is about the initial copy into iCloud. If you have an existing store (before iCloud integration is added) with user information, how are you handling seeding it to the iCloud store?