How to pass Core Data objectID and use it with Continuity

189 Views Asked by At

Just trying to update some Core Data apps with Continuity and have run into a bit of an issue with using the selected objects ID in the userInfo dictionary to display the correct data on the continuing device.

My first thought was to use the ObjectID, however on the receiving device this would never find a corresponding object in the Core Data store.

As it turns out the URL representation of the objectID contains the UUID of the store itself, and because the two stores UUID's are different this is obviously going to fail.

So I guess I could replace the Core Data store's UUID in the URL with the continuing devices UUID and use this, and no doubt it would work.

The Url seems to be of the following format

Does anyone know what the correct way would be to pass a reference to an object between two devices with core data stores that are synchronised via iCloud?

1

There are 1 best solutions below

1
On

I'll answer this one myself and see if there are any better answers...

I pass the url of the objectID (from objectID.URIRepresentation) using Continuity API and on the receiving device create a new URL using the following:

url is the url passed in the NSUserActivity.userInfo dictionary

let storeUUID = self.identifierForStore()

// Switch the host component to be the local storeUUID
let newURL = NSURL(scheme: url.scheme!, host: storeUUID, path: url.path!)


func identifierForStore()->NSString? {
    if let store = self.persistentStoreCoordinator?.persistentStores[0] as? NSPersistentStore {          
        return store.identifier
    } else {
        return nil
    }
}

This seems to work just fine - hope it helps someone