UIManagedDocument does not update changes of relationships immediately
-
11-06-2021 - |
Question
I'm using an UIManagedDocument in my project and have to change the relationship of existing objects.
All objects already exist in the database so I'm not dealing with the temporaryID/permanentID issue. I'm also almost using updateChangeCount:
on my UIManagedDocument to save changes. So it shouldn't be an issue with the UIManagedDocument's usual suspects.
Model:
Company
|-- Department
|-- Employee
Operation:
Move a Department
from one Company
to another by calling [aDepartment setCompany:newCompany]
.
Situation 1 (succeeds):
Execute a fetchRequest on entity Department
calling company = newCompany
returns the expected result.
Situation 2 (fails):
Execute a fetchRequest on entity Employee
calling department.company = newCompany
returns no result.
Situation 2 only succeeds after UIManagedDocument auto-saves.
Any ideas how to solve this problem?
Solution
After dealing with this issue for quite a long time - unfortunately even Apple engineers weren't able to help - I was able to find a solution.
Calling [myManagedDocument saveToURL:fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];
after changing the relationships solves the problem and returns the correct state immediately.
Anyway, I was filling a bug report for this to get this done via updateChangeCount:
.
OTHER TIPS
the same issue. I finally find a solution by saving both managedObjectContext and it's parent context.
NSError *error;
if ( ![self.fetchedResultsController.managedObjectContext save:&error] ||![self.fetchedResultsController.managedObjectContext.parentContext save:&error]) {
// .....
}