NSManagedObject的唯一标识符 [英] Unique Identifier for NSManagedObject
问题描述
我需要获取一个 NSManagedObject
类型的唯一标识符。
这会排除 NSManagedObjectID
-hash
方法可能是非唯一的。 真正想避免创建一个无用的uniqueIdentifier UUID字段上的实体,因为这似乎是浪费和凌乱。这里是否有一个公认的最佳做法?
尝试 URIRepresentation
属性 NSManagedObjectID
。这是当前 NSManagerObject
的非常唯一的ID,但要小心,直到 NSManagedObject
未保存它仅为您提供临时ID ,而不是永久ID,它们可能不同。
> UPDATE#1
此不是一个虚构的唯一ID,这是纯每个个人的URL NSManagedObject
(像每个文件都有一个唯一的URL),使用它们你可以再次找到原始的 NSManagedObject
,之后你失去了他们的指针。我知道很难理解,但这是 NSManagedObjectID
及其属性的点。
(如果你不明白 CoreData
及其对象是如何工作的,你不会downvote的答案,请阅读更多的文档,而不是毫无意义的downvoting。 )
更新#2 到@NickLocking的注释,我会扩展上面我的答案的粗体部分:
,直到保存 c code NSManagedObjectContext 用于新的仍未保存的类 c> 仅具有临时唯一ID。他们将在第一次保存后获取永久唯一ID。
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?
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.
这篇关于NSManagedObject的唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!