0
votes

I've having an issue where CoreData is unable to fulfill a fault on an object. I assume it's because the object has been deleted and now CoreData is trying to access a property in some context and can't fault it in. To resolve this I attempted to call setRelationshipKeyPathsForPrefetching and pass it the relationships that I want to have prefetched.

Example, I have an Entity A, and it has a to-many relationship to Entity B, but at some point while I'm accessing properties on Entity B, Entity B is deleted in another context and now a fault can't be fulfilled.

[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"listOfBs", nil]];

I thought that if I did this, it would prefetch Entity B so that I can call its properties safely but this doesn't seem to be the case.

EDIT: I can't seem to find any info about this but it must be a fairly common problem. If some context has a managed object that is deleted in another context, it should have some way to safely figure out that the object no longer exists rather than just crashing

EDIT 2: I don't think CoreData is properly acknowledging my prefetch request. If I call setRelationshipKeyPathsForPrefetching than the objects that are returned should be faulted in yet even in the case where I don't have an error, I can simply print out the object that get returned and see that they are faulted. Does CoreData not prefetch objects in to-many relationships?

EDIT 3: Ok so perhaps the relationships themselves are prefetched but the properties on the prefetched items are not faulted in. So if I have Entity A and I want to prefetch all of the B's associated with A, I use setRelationshipKeyPathsForPrefetching but all of the properties of the B's are not faulted in.

1
Can you describe the context when this is happening. And you have only one NSManagedContext and you are always on the same thread?Vincent Bernier
I have multiple threads/contexts. Furthermore I have multiple processes which are touching the DB. There are definitely scenarios where I could be looking at something that no longer exists.JPC
I'm not use to use CoreData on multiple thread, it was discourage to make modification on multiple thread. In know that in the latest release Appel have made some modification to make it simpler to deal with multiple thread, so my best guess is to what the Tech Talk about that on the Appel Developer web site.Vincent Bernier

1 Answers

0
votes

Figured out a solution. Core Data doesn't allow atomic fetches from the database so if something after prefetching the relationships, Core Data would throw an error. Prefetching the relationships does just that, prefetches the relationships. But not the data. The solution was to use a try/catch and refresh the object on an exception.