Core Data returns a different object instance for

2019-05-06 15:35发布

问题:

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.