将一个实体的NSManagedObjectID保存为另一实体的属性? [英] Saving the NSManagedObjectID from one entity as a property on a different Entity?
问题描述
我有一个核心数据实体,需要保留其他某些实体的 NSManagedObjectID
。为此,我正在考虑将ObjectID转换为字符串,该字符串是 NSManagedObject
属性的批准类型。
我可以从文档中读取到我可以通过以下方式获取ID的URI表示形式:
NSURL * uriID = [[myEntity objectID ] URIRepresentation];
然后我可以通过以下方式将此URL转换为NSString:
NSString * stringID = [uriID absoluteString];
这可以坚持到我的 NSManagedObject
' s NSString属性。
现在当我需要换行时会发生什么?
我想能够执行以下操作:
if([myManagedObject objectID] == value)
其中, 值 是 NSManagedObjectID
我早些时候转换为NSString。
要进一步说明原因:我需要能够保留Entity对象并保持另一个实体对象的ObjectID,以便我以后可以使用:这个对象的上一次交互是与此实体。
希望有人可以帮助我使它正常工作:)
谢谢
为什么不只是在 Object
称为 interaction
,它指向 Entity
的实例,反之诗篇中,从 Entity
到 Object
的一对多关系称为 interactions
?
这可以很好地解决问题。
但是您也可以看看 code>
和 + URLWithString:
方法可用于其他方向。
I have a Core Data Entity that needs to hold onto the NSManagedObjectID
of some other Entity. To do so I was considering converting the ObjectID to a string that is an approved type of an NSManagedObject
attribute.
I can read from the documentation that I can get a URI representation of the ID by:
NSURL *uriID = [[myEntity objectID] URIRepresentation];
I can then convert this URL to an NSString by:
NSString *stringID = [uriID absoluteString];
This I can persist to my NSManagedObject
's NSString attribute.
Now what happens when I need to go the other way?
I would like to be able to do something like this:
if([myManagedObject objectID] == value)
where value is the NSManagedObjectID
that I converted to an NSString earlier.
To shed a little more light on the why: I need to be able to have an Entity object hold and persist the ObjectID of another Entity object, so that I later on can go: this Objects last "interaction" was with this Entity.
Hope someone can help me get this working:) Thank you
Why not just establish a to-one relationship property in Object
called interaction
, which points to an instance of an Entity
— and vice verse, a to-many relationship from Entity
to Object
called interactions
? This solves the problem pretty neatly, without all the conversion methods.
But you might also look at the -managedObjectIDForURIRepresentation:
and +URLWithString:
methods to go the other direction.
这篇关于将一个实体的NSManagedObjectID保存为另一实体的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!