Matt Wilding
Matt Wilding

Reputation: 20153

Are NSManagedObject objectIDs unique across space and time like a CFUUID?

The NSManagedObjectID documentation states:

An NSManagedObjectID object is a compact, universal, identifier for a managed object. This forms the basis for uniquing in the Core Data Framework. A managed object ID uniquely identifies the same managed object both between managed object contexts in a single application, and in multiple applications (as in distributed systems).

Translation in my head: "There is probably no way that any two NSManagedObjectIDs are ever the same across the set of all instances of my application."

The CFUUID documentation states:

UUIDs ... are 128-bit values guaranteed to be unique. A UUID is made unique over both space and time by combining a value unique to the computer on which it was generated—usually the Ethernet hardware address—and a value representing the number of 100-nanosecond intervals since October 15, 1582 at 00:00:00.

Translation in my head: "There is definitely no way that any two CFUUIDs are ever the same across the set of all instances of my application."

The fact that NSManagedObjectIDs are described as a "universal identifier" makes me almost certain that they offer the same uniqueness as a CFUUID, whereas "unique across space and time" leaves absolutely no room for doubt. Can anybody with more Core Data experience than me confirm or deny my thoughts?

Upvotes: 1

Views: 1162

Answers (1)

Brad Larson
Brad Larson

Reputation: 170319

Beyond uniqueness, there is one case where the object ID will change, and that's if you query it before persisting the object to disk. After saving, it will have a different ID. Beyond that point, the ID will not change. I just wanted to point this out because it caused me a bit of confusion until I figured out what was happening.

I can't comment on the hashing used to generate the NSManagedObjectID, but it does seem like the odds of it matching another NSManagedObject are vanishingly small, based on looking at the IDs generated.

Upvotes: 2

Related Questions