1

I'm adding iCloud syncing to my app. My UIManagedDocument store is in the sandbox, and its change log is in iCloud (specified by NSPersistentStoreUbiquitousContentURLKey). The problem is that when I install and run the app for the 1st time on a 2nd device, it takes many minutes to do the initial sync (to pull the updates created by the 1st device). This is a real issue because that sync is evidently happening inside UIManagedDocument:saveToURL:completionHandler. So the completion handler block takes a long time to get called, and if I access my UIManagedDocument before UIManagedDocument:saveToURL returns, I understandably get a crash.

I want to open the database quickly and then do the sync in the background. How can I do it?

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76

2 Answers2

3

Yes, the initial sync on a second device can take 90 seconds or more (iOS 7). I have a working open-source example of an iOS 7 app that creates instances of UIManagedDocument. See: https://github.com/DonBriggs/MultiDocumentPlusUUID. First, see the screenshots of the app in operation on two devices. If the screenshots address your issue, you might try the app yourself and have a look at the code. I'm eager for feedback.

Don
  • 31
  • 2
0

You mention "database" and tagged this question with CoreData. However, you're referring to UIManagedDocument, which is synced differently than CoreData databases. If your database was CoreData, you would initialize it with the new NSManagedObjectContext concurrency API. Take a look at:

[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

I've used this successfully to sync a CoreData database asynchronously, but I'm not sure how well it applies to UIManagedDocuments.

melsam
  • 4,947
  • 1
  • 22
  • 23
  • 1
    I'm not sure what you mean by saying that `UIManagedDocument` is synced differently from CoreData databases. The purpose of UIManagedDocument is to simplify CoreData store access (see http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIManagedDocument_Class/Reference/Reference.html). Your solution doesn't seem to directly apply because with UIManagedDocument you don't init the NSManagedObjectContext yourself. – TotoroTotoro May 20 '12 at 22:50