Unique Identifier for NSManagedObject
Asked Answered
M

3

10

I have a need to obtain a unique identifier for a type of NSManagedObject I've created. It needs to be available as soon as the object has been created, never change, and be completely unique.

This rules out the NSManagedObjectID, as this can change when the context is saved. I believe the -hash method could be non-unique if my objects have the same properties.

I'd really like to avoid creating an otherwise useless uniqueIdentifier UUID field on the entity as this seems wasteful and messy. Is there an accepted best practice here?

Mcguinness answered 19/7, 2012 at 4:53 Comment(2)
Have you considered a salted hash?Zealot
the NSManagedObjectID will be change after the first saving of the context then the object will get the final NSManagedObjectID, and it won't be changed ever. you should not worry about the exists IDs then, not even you deleted the object because no new object will get the old ID.Rashad
R
13

try the URIRepresentation property of NSManagedObjectID. this is very unique ID for the current NSManagerObject but be careful until the NSManagedObject is not saved it gives you a temporary ID only, not a permanent one and they might be different. (I'm just saying it because I don't know for what and how you want to use the unique ID.)

UPDATE #1

this is not an imaginary unique ID only, this is pure unique URL for each individual NSManagedObject (like every file has a unique URL), using them you can find again the original NSManagedObject, after you lost their pointer. I know it is hard to understand, but this is the point of the NSManagedObjectID and its properties.

(if you don't understand how the CoreData and their objects work, you would not downvote the answer. please, read more documentation instead of the pointless downvoting.)

UPDATE #2

according to @NickLocking comment, I would extend the bold part of my answer above:

until saving the NSManagedObjectContext for the the new and still unsaved NSManagedObject classes has a temporary unique ID only. They will get the permanent unique ID after they are saved at first time.

Rashad answered 19/7, 2012 at 8:48 Comment(1)
The URIRepresentation changes when the object is first saved.Mcguinness
M
3

Eventually I have decided that there is no good way to do this, so I just created a uniqueIdentifier field that I apply a UUID to on awakeFromInsert.

Saving the object causes other parts of my application, specifically NSFetchedResultsControllers, to update before I'm finished with the object. I briefly tried NSManagedObjectContext's obtainPermanentObjectIds:withError: method, thinking it would obtain the object IDs without saving the context, but in fact it does simply save the context.

Mcguinness answered 20/7, 2012 at 3:54 Comment(0)
C
1

The only unique identifiers provided automatically by CoreData is the object ID, but as you have noted it will change after it is initially created. But before you go coming up with another way to work around this, you might want to consider defining something like the following in your managed object class:

- (NSManagedObjectID *)permID {
    if ([[self objectID] isTemporaryID]) {
        // Save myself, returning nil if there are errors
    }
    return [self objectID];
}

This approach isn't perfect by any means, especially if you need to expose the permanent ID before the object is in a state where it is valid and can be saved to the database. But it will allow you to expose a permanent ID in a consistent way as long as you don't need it before the object can be saved.

Contrary answered 19/7, 2012 at 15:51 Comment(1)
The trouble with this is, saving the object in this instance means other parts of my application detect that a new object has been created, and process it. This means, for instance, that a tableview entry is created with no fields set.Mcguinness

© 2022 - 2024 — McMap. All rights reserved.