简体   繁体   English

核心数据:对象ID更改

[英]Core data: Object ID changes

I'm trying to fetch object by object ID in a simple core data implementation. 我试图在一个简单的核心数据实现中按对象ID提取对象。 Using this api 使用这个API

let targetObj = CoreDataManager.sharedInstance.privateQueueContext.object(with: self.objectID) as! MyObj

but it seems object id changes. 但是对象ID似乎发生了变化。 Here is the console log 这是控制台日志

(lldb) po self.objectID
0xd000000000280000 <x-coredata://551DDB76-537D-41FA-B923-F772E5EE5D29/MyObj/p10>

(lldb) po CoreDataManager.sharedInstance.privateQueueContext.object(with: self.objectID).objectID
0xd000000000280006 <x-coredata://551DDB76-537D-41FA-B923-F772E5EE5D29/MyObj/p10>

Please let me know if I'm doing anything wrong. 如果我做错任何事,请让我知道。 Actually I'm book keeping these objectIds in an queue and need to dequeue the object based on these objectIds . 实际上,我正在将这些objectIds保留在队列中,并且需要基于这些objectIds使对象出队

Thanks Ankit 谢谢安吉

The only difference you're seeing when you print each object ID is the address in memory where the object ID is stored. 打印每个对象ID时唯一看到的区别是存储对象ID的内存中的地址。

So, if you take those two objects and compare them with == they will be different, because that is testing referential equality. 因此,如果您将这两个对象与==进行比较,则它们将有所不同,因为这是在测试参照相等性。

If you compare those two objects with isEqual: , it will return true, because isEqual: is testing whether the values they each represent are equal. 如果将两个对象与isEqual:进行比较,则它将返回true,因为isEqual:正在测试它们各自表示的值是否相等。

You can't reliably compare objects by reference, you need to use isEqual: . 您不能通过引用可靠地比较对象,需要使用isEqual:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM