I have a Core Data Model consisting of a Trail that has-many Locations. I am also using RestKit. My code to get the trail looks like this:
Trail *trail = [Trail findFirst];
My problem is that this returns a different Trail every time. Three successive calls to [Trail findFirst] return:
<Trail: 0xf489c70> (entity: Trail; id: 0xf489a60 <x-coredata://77BCA7AD-4C71-437D-8A7C-C047B8E64567/Trail/p1> ; data: <fault>)
<Trail: 0xf476ca0> (entity: Trail; id: 0xf489110 <x-coredata://77BCA7AD-4C71-437D-8A7C-C047B8E64567/Trail/p1> ; data: <fault>)
<Trail: 0xf48c660> (entity: Trail; id: 0xf48c430 <x-coredata://77BCA7AD-4C71-437D-8A7C-C047B8E64567/Trail/p1> ; data: <fault>)
If I do [Trail allObjects] I see a single Trail item which will be returned the next time I call [Trail findFirst]. If I do [Trail allObjects] again I see a different trail object which will be returned the next time I call [Trail first];
This is bending my mind. How can CoreData be returning a different entity each time?
You’re looking at the wrong ID. The important one is
x-coredata://77BCA7AD-4C71-437D-8A7C-C047B8E64567/Trail/p1, all of which show the same number.The ID you’re looking at is the address of the object(s). They are different objects but they point to the same underlying Core Data object.
Think of a hypothetical number object. I create one and I put the number 1 in it. I create another number object and put a 1 in that one too. The objects are equal (both contain 1) but they not the same (different objects with different addresses).