How to ensure that the property of the source data is retrieved from the context, and not from the cache?

I have a managed object model containing 2 objects.

One of the objects (Lets call it EA ) calculates its properties, referring to some properties in a subset of the second object (name it EB ).

Inside EA, I created a “Corrected Property” with a predicate that returns a subset of the EB objects that I need.

When I delete, insert an EB object or modify an EB object , I use notifications to make sure that EA is kept up to date. So, for example, after "saving" the EA will recalculate its properties.

My problem is that when I access the selected property (which I do inside the EB class with [self valueForKey:FetchedPropertyKeyName]), it appears only to retrieve the cached version, in other words, the first time it’s good, but when I add another EA object, I do not see it in the returned array when I access the selected property. This is most obvious when I delete an object, since then I get a failure due to the fact that this object no longer exists.

The documentation for Fetched Properties says:

If the objects in the target are changed, you must re-evaluate the selected property to ensure its relevance. You use refreshObject: mergeChanges: to manually update the properties - this leads to the repeated execution of the query for the selection associated with this property when the next object error completes.

Sorry if this sounds like a dumb question, but unfortunately I don’t see where I should call refreshObject:mergeChanges:??

+3
source share
1 answer

The discipline of writing the question made me look at the code again, so now I have a solution!

I added:

[[self managedObjectContext] refreshObject:self mergeChanges:YES];

. , awakeFromFetch, , , , , .

+10

Source: https://habr.com/ru/post/1766218/


All Articles