CoreData - Can not add relation after fulfilled fault
I am working with two managed object contexts; a read only frontend contexts and another backend context that is used to manipulate objects.
In the code block, the second assert fails. If I comment out the first assertion, the second one will be ok. So, I have narrowed it down a bit. It seems that when I have accessed the property productAccess, so the fault is fullfilled(?), I can not add additional access objects to the user, even if I do a new fetch in frontend context.
- (void)testAddingAccesses{
[self addAccessToUser];
[self addAccessToUser];
UserMO *newFront = [UserMO getUser];
STAssertOperation([newFront.productAccesses count], 2u, ==, @"Should be two!!");
[self addAccessToUser];
UserMO *another = [UserMO getUser];
STAssertOperation([[another.productAccesses allObjects] count], 3u, ==, @"Should be three!!");
}
More details
[self addAccessToUser]
Picks up the one and only UserMO and adds an AccessMO (in backend context), then saves background context.
[UserMO getUser]
Will return the one and only user in frontend context.
Why does not changes get reflected after the first time I access the frontend productAcc开发者_如何学JAVAesses?
Found the answer!
If you are using two context, you do need to refresh the object to get the most recent changes in store.
[frontendContext refreshObject:user mergeChanges:YES];
精彩评论