Search code examples
swiftcore-dataensembles

iCloud syncing of Core Data with Ensembles


My plan is to purchase Ensembles 2 (to take advantage of speed/efficiencies etc) but am trying to make sure that I will be able to get it to work (in a test Swift project) first. To do this I am experimenting with v1.

Using the Simple Sync with Swift as a guide I have incorporated ensembles into my xcode project.

The data from the app does appear to be getting stored in iCloud as when I delete the app and then re-add it, leeching and then syncing does restore the correct data from iCloud. My trouble is that testing with a second device (signed in to the same apple/iCloud account) does the same thing with its own data. The data from the 2 devices is never merged. However the data created on each device is restored to its own device after reloading the app.

Does anyone know how this could be?

Am wondering if the problem might be the store url that I am generating. A lot of the Core Data Stack set up is now done automatically in Swift 3+ (NSPersistentContainer) and so these things do not need to be generated by the user. Here is how i am generating the variables for store url and model url to use when setting up my ensemble:

var storeDirectoryURL: URL {
        return try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    }

var storeURL: URL {

        return storeDirectoryURL.appendingPathComponent("VsSyncTest.sqlite")
    }

    // Setup Ensemble

let modelURL = Bundle.main.url(forResource: "VsSyncTest", withExtension: "momd")

Solution

  • The only way I can imagine that would happen is if either you are using different iCloud accounts, or you have a different ensemble identifier for each device.

    Note also that the iCloud document sharing can be stubborn. Just because the files get added to the container locally does not mean they will immediately transfer to the other device. (CloudKit backend is much better in that respect, but only in E2.)

    If you are testing with the simulator, using Debug > iCloud > Trigger Sync in Simulator may help.