数据库中行的 Core Data 主键 ID

发布于 2024-10-12 09:13:40 字数 134 浏览 8 评论 0原文

假设我有一个存储在 Core Data 中的书籍列表。我想通过主键 ID 搜索一本书。

我知道 Core Data 创建的 sqlite 文件在每个表中都有一个 ID 列,但这似乎并没有以任何方式暴露给我。

有人有什么建议吗?

Suppose I have a list of books stored in Core Data. I want to search for a book by it's primary key ID.

I know the sqlite file created by Core Data has an ID column in each table, but this doesn't seem to be exposed to me in anyway.

Does anyone have any recommendations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仙气飘飘 2024-10-19 09:13:40

-[NSManagedObject objectID] 是 Core Data 中对象实例的唯一 ID。它可以通过 -[NSManagedObjectID URIRepresentation] 进行序列化。您可以使用 -[NSPersistentStoreCoordinator ManagedObjectIDForURIRepresentation:] 从持久存储协调器检索 objectID,然后使用 -[NSManagedObjectContext objectWithID:] 从托管对象上下文获取对象: ]

但是

您应该记住,核心数据不是 ORM。它是一个对象图管理框架。也就是说,使用 SQLite(和唯一行 ID)作为后端纯粹是一个实现细节。您越早摆脱 SQL/RDBMS 思维模式,您就会越快对 Core Data 感到满意。不要尝试从存储的 ID 中查找对象,而是考虑为什么需要该对象以及什么对象需要它。如果 Foo 类的实例需要能够访问 Bar 类的实例,为什么不直接创建一个从 FooBar 并将相应的 Bar 实例设置为相应 Foo 实例上关联的目标。让 Core Data 跟踪对象 ID。

-[NSManagedObject objectID] is the unique ID for an object instance in Core Data. It can be serialized via -[NSManagedObjectID URIRepresentation]. You can retrieve the objectID from a persistent store coordinator with -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:] and then get the object from a managed object context with -[NSManagedObjectContext objectWithID:].

BUT

You should keep in mind that Core Data is not an ORM. It is an object graph management framework. That is uses SQLite (and unique row IDs) as a backend is purely an implementation detail. The sooner you can get yourself out of the SQL/RDBMS mindset, the faster you will be happy with Core Data. Instead of trying to find an object from a stored ID, consider why you need that object and what object needs it. If an instance of class Foo needs to be able to get to an instance of class Bar, why not just create an association from the Foo to the Bar and set the appropriate Bar instance as the target of the association on the appropriate Foo instance. Let Core Data keep track of object IDs.

强辩 2024-10-19 09:13:40

正如 Barry Wark 所说,永远记住核心数据不是一个 orm。纯 SQL 详细信息不会向用户公开,每一行只是一个对象。顺便说一句,有时您应该需要访问“主键”,例如当您需要将 coredata 数据库与外部 sql 数据库同步时(在我的例子中,我需要在回调函数中使用它来更改 INSERT 后对象的状态)它在远程数据库中成功)。在这种情况下,您可以使用:

objectId = [[[myCoredataObject objectID] URIRepresentation] absoluteString]

它将返回一个字符串,例如:x-coredata://76BA122F-0BF5-4D9D-AE3F-BD321271B004/Object/p521,这是Core Data使用的唯一ID来识别该对象。

如果您想取回具有该唯一 ID 的对象:

NSManagedObject *managedObject = [managedObjectContext objectWithID:[persistentStoreCoordinator managedObjectIDForURIRepresentation:[NSURL URLWithString:objectId]]];

注意:请记住,如果接收者尚未保存在 CoreData Context 中,则对象 ID 是一个临时值,在保存对象时该值将会更改。

As Barry Wark said, remember always that Core Data is not an orm. Pure SQL details are not exposed to the user and every row is just an object. By the way, sometime you should need to access the "primary key", for example when you need to sync the coredata db with external sql databases (in my case I needed it in a callback function to change the state of an object after INSERT it with success in the remote db). In this case, you can use:

objectId = [[[myCoredataObject objectID] URIRepresentation] absoluteString]

that will return a string like: x-coredata://76BA122F-0BF5-4D9D-AE3F-BD321271B004/Object/p521 that is the unique id used by Core Data to identify that object.

If you want to get back an object with that unique id:

NSManagedObject *managedObject = [managedObjectContext objectWithID:[persistentStoreCoordinator managedObjectIDForURIRepresentation:[NSURL URLWithString:objectId]]];

NB: Remember that if the receiver has not yet been saved in the CoreData Context, the object ID is a temporary value that will change when the object is saved.

野味少女 2024-10-19 09:13:40

这是您可以使用 SwiftNSManagedObject 获取 object id 作为 String 的方法:

entity.objectID.uriRepresentation().absoluteString

This is the way you can get the object id as String using Swift from a NSManagedObject:

entity.objectID.uriRepresentation().absoluteString
两人的回忆 2024-10-19 09:13:40

在 Swift 中,这将通过获取行的 ID 作为 URI 然后获取 URI 的最后一个路径来完成。

entity.objectID.uriRepresentation().lastPathComponent

最后一个路径的输出将如下所示,

p12

此输出是字符串,因此您可以使用以下方法删除 p

trimmingCharacters()

// Like this
let id = entity.objectID.uriRepresentation().lastPathComponent.trimmingCharacters(in: ["p"])

in Swift this will be done by getting ID of the row as URI then get last path of URI

entity.objectID.uriRepresentation().lastPathComponent

the output of last path will look like this

p12

this output is string so you can remove the p using:

trimmingCharacters()

// Like this
let id = entity.objectID.uriRepresentation().lastPathComponent.trimmingCharacters(in: ["p"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文