mluisbrown / iCloudCoreDataStack

A persistence stack for using Core Data with iCloud Sync in iOS7
126 stars 11 forks source link

Initial Copy question #6

Open GoBananas opened 10 years ago

GoBananas commented 10 years ago

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?

mluisbrown commented 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.

lehn0058 commented 10 years ago

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?

peterknapp commented 10 years ago

What I did (in a nutshell):

  1. Copy all objects from iCloud store into a NSData structure locally.
  2. Changing the store from iCloud to local Core Data Store
  3. Create new objects from NSData structure to local Core Data Store
  4. I left the iCloud store "undeleted" so if the user want to go back, he will be presented with the "old" content.

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.

mike38217 commented 10 years ago

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:

But it doesn't work for me. Is there way to get notification when migration process has finished?

mluisbrown commented 10 years ago

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.