I actually have a use case for this, which causes a problem. Here's an example with a ToDo app:
Let's imagine in our datamodel we have 2 types of objects: Task (fields: creationDate, name) and Tag (fields: creationDate, name). A Task is the actual todo and a Priority (ex name: urgent, normal, idle) is associated to a Task.
In our use case, a Task always has a default Priority with name "urgent".
Priority objects are not created in advance (lazy created).
The unicity of an object is defined by NSManagedObjectID which is a randomly generated value.
First example that works:
- On DeviceA, I tap on the "Create a task" button.
- Task1 is created, since Priority with name "urgent" doesn't exist, it is created (Priority1) and associated to the newly created task object.
- A few seconds later I look at DeviceB, I see the task I created on DeviceB
- I tap on the "Create a task" button.
- Task2 is created, since Priority with name "urgent" exists (Priority1), I just associate it to the Task2 object
--> I have 3 objects: Task1, Task2, Priority1
Second example that doesn't work:
- On DeviceA and DeviceB, I tap on the "Create a task" button at the same time.
- On DeviceA: Task1 is created, since Priority with name "urgent" doesn't exist, it is created (Priority1) and associated to the newly created task object.
- On DeviceB: Task1 is created, since Priority with name "urgent" doesn't exist, it is created (Priority2) and associated to the newly created task object.
---> I have 4 objects: Task1, Task2, Priority1, Priority2
---> Since, before they have time to sync, Devices don't know that another device maybe be creating a Priority object, they all create one. And Since NSManagedObjectID i randomly generated, so not deterministic, they functionally are the same object, but they're not technically.
- for unicity of object, Ensembles lets you control what unique ID to give to an object. More precisely, when an object is created, a delegate is called and ask you "give me a unique ID for this object". And here you have the choice to return NSManagedObjectID, or the name property of the object. Me, I added a clientID property to all objects (which was set in awakeFromInsert method with a UDID I generate), so I returned the clientID to Ensembles for each object.
- the client ID was: "typeOfTheObject_theUDID" (ex: "task_1717177171-19191919191-11818JDJDJDJ-DJDJDJ").
- So, for such a case I would do this hack: the clientID of Priority would be the UDID of the task creating it but replacing "task_" by "_priority_" (ex: priority_1717177171-19191919191-11818JDJDJDJ-DJDJDJ)
---> this make it deterministic and when DeviceB receives a Priority object with the same clientID as the one it has, it knows it's the same object
I'm sorry for this long message, it's hard for me to explain this simply :(
Thank you :)
Florion