使用NSManagedObjectID URI的简洁形式? [英] Using a concise form of NSManagedObjectID URI?
问题描述
在我的应用程序中,我使用Core Data和一个不使用Core Data的附加sqlite数据库。在这个额外的数据库中,我有列通过每个实例的 NSManagedObjectID
存储对 NSManagedObject
实例的引用。
In my app, I'm using Core Data alongside an additional sqlite database that doesn't use Core Data. In this additional database, I have columns that store references to NSManagedObject
instances via each instance's NSManagedObjectID
.
我得到一个实例的 objectId
作为存储的字符串,如下所示:
I'm getting an instance's objectId
as a string for storage like this:
instance.objectID.URIRepresentation.absoluteString
在字符串中看起来像:
x-coredata://EE13EA1E-D5F4-4E38-986D-3F4B0B03AEE4/ClassName/p658
我稍后可以使用它来获取 NSManagedObject
instance like this:
Which I can later use to fetch the NSManagedObject
instance like this:
[persistentStoreCoordinator managedObjectIDForURIRepresentation:[NSURL URLWithString:uriString]];
由于这些URI字符串是冗长的并且包含冗余信息,我想保存的唯一方面每个为了节省数据库中的空间和提高查询性能。所以在上面的例子中,只是'658'而不是整个URI字符串。
As these URI strings are verbose and contain redundant information, I would like to save just the unique aspect of each in order to conserve space in the db and improve query performance. So in the example above, just '658' rather than the whole URI string.
所以第一个问题是:什么是一个很好的方法, NSManagedObjectID
?第二,一旦我有存储,我怎么能以后使用它来获取实例?
So the first question is: what's a good way to extract just the unique tail of an NSManagedObjectID
? And secondly, once I have that stored, how can I later use it to fetch the instance?
我想避免字符串操作,因为感觉icky,如果它是唯一的方式,我们会考虑。我唯一的困惑是有'EE13EA1E-D5F4-4E38-986D-3F4B0B03AEE4'部分来自上面的例子。如何访问该值以重建有效的URI?
I would like to avoid string manipulation as that feels icky, but I'll consider it if it's the only way. My only confusion there is where the 'EE13EA1E-D5F4-4E38-986D-3F4B0B03AEE4' portion is coming from in the above example. How could I access that value in order to rebuild a valid URI?
推荐答案
否则,对于包含大块数据的属性, URIRepresentation
返回 NSURL
,因此不需要icky字符串操作。只需使用 NSURL
方法。 ; ^)下面是如何打破它:
Otherwise, URIRepresentation
returns an NSURL
, so no "icky" string manipulation is necessary. Just use the NSURL
methods. ;^) Here's how you break it down:
NSURL *instanceURL = instance.objectID.URIRepresentation;
NSURL *classURL = [instanceURL URLByDeletingLastPathComponent];
NSString *classString = [classURL absoluteString];
NSString *instanceId = [instanceURL lastPathComponent];
以下是如何将它重新组合在一起:
And here's how you put it back together later:
NSURL *reconstructedClassURL = [NSURL URLWithString:classString];
NSURL *reconstructedInstanceURL = [reconstructedClassURL
URLByAppendingPathComponent:instanceId];
NSManagedObjectID *objectID = [moc.persistentStoreCoordinator
managedObjectIDForURIRepresentation:reconstructedInstanceURL];
NSManagedObject *reconstructedInstance = [moc objectWithID:objectID];
注意,由于 URIRepresentation
可存档,从Core Data为您提供的组件重建数据没有任何危害。核心数据不知道你把它分开,并把它放在一起。
Note that since the URIRepresentation
is documented to be "archivable", there's no harm in rebuilding one from components that were given to you by Core Data. Core Data doesn't know that you took it apart and put it back together.
但是,Apple允许更改 URIRepresentation返回的格式
,只要 managedObjectIDForURIRepresentation:
继续接受旧格式即可。这意味着上面的代码分裂 URIRepresentation
可能有可能会停止工作。
However, Apple is allowed to change the format returned by URIRepresentation
in the future, as long as managedObjectIDForURIRepresentation:
continues accepting the old format. That means the code above that breaks apart the URIRepresentation
could stop working someday.
这篇关于使用NSManagedObjectID URI的简洁形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!