Core Data returns a different object instance for the same NSManagedObject each time I fetch it. Why is this?

旧城冷巷雨未停 提交于 2019-12-07 09:12:08

问题


I have recently noticed that if I fetch a ManagedObject of which I can verify that there is only one in the model and which is not retained anywhere in my application, every time the fetch returns the object it is a different instance (with a pointer to a different memory adress). Why is this?


回答1:


If no one retains it, Core Data is free to release it. If you ask for it again, it will probably be at a different memory location. You can't count on it being the same object instance.




回答2:


A given NSManagedObject is tied to a specific NSManagedObjectContext, so if you change contexts, you will necessarily change objects. If the object faults, you may also receive a new instance if the MOC has released the object. This is part of how Core Data keeps memory usage low.



来源:https://stackoverflow.com/questions/10938229/core-data-returns-a-different-object-instance-for-the-same-nsmanagedobject-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!