xcode 核心数据 - 数据库中一行的主键 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4720182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:39:56  来源:igfitidea点击:

core data - the primary key id of a row in the database

iphonexcodecore-datanspredicatensmanagedobject

提问by WoodenKitty

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.

假设我有一个存储在 Core Data 中的书籍列表。我想通过它的主键 ID 搜索一本书。我知道 Core Data 创建的 sqlite 文件在每个表中都有一个 ID 列,但这似乎并没有暴露给我。

Does anyone have any recommendations?

有人有什么建议吗?

Thanks!

谢谢!

回答by Barry Wark

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

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

BUT

You should keep in mind that Core Data is notan 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 Fooneeds to be able to get to an instance of class Bar, why not just create an association from the Footo the Barand set the appropriate Barinstance as the target of the association on the appropriate Fooinstance. Let Core Data keep track of object IDs.

您应该记住,Core Data不是ORM。它是一个对象图管理框架。即使用 SQLite(和唯一行 ID)作为后端纯粹是一个实现细节。你越早摆脱 SQL/RDBMS 的心态,你就会越快对 Core Data 感到满意。与其尝试从存储的 ID 中查找对象,不如考虑为什么需要该对象以及哪个对象需要它。如果 class 的实例Foo需要能够到达 class 的实例Bar,为什么不创建一个从Foo到 的关联Bar并将适当的Bar实例设置为适当实例上的关联目标Foo。让 Core Data 跟踪对象 ID。

回答by Donnit

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:

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

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 coredata to identify that object.

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

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

如果要取回具有该唯一 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.

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