Brett
Brett

Reputation: 21

iOS Core Data "optimistic locking failure"

I've got a really frustrating problem that I'd like help understanding and perhaps even fixing.

I'm learning Core Data by building a rather simple app.

My model is as follows:

User

Attributes

Relationships

AgeGroup

Attributes

Relationships

ListItem

Attributes

Relationships

My program is set up as follows:

id navigationController = [[self window] rootViewController];
id controller = [navigationController topViewController];
[controller setManagedObjectContext:[self managedObjectContext]];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"selectprofile"]) {
        [[segue destinationViewController] setManagedObjectContext:[self managedObjectContext]];
    }
}
AddProfileViewController *viewController = (AddProfileViewController *)[[segue destinationViewController] topViewController];
[viewController setManagedObjectContext:self.managedObjectContext];
User *newMO = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managedObjectContext];

newMO.name = self.nameTextField.text;
newMO.dob = _dob;
// etc
NSFetchRequest *ageGroupRecordRequest = [NSFetchRequest fetchRequestWithEntityName:@"AgeGroup"];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                      ascending:YES];
[ageGroupRecordRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

// Make a predicate to find the correct AgeGroup based on what was calculated
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(title == %@)", ageGroup];
[ageGroupRecordRequest setPredicate:predicate];

// Run the fetch request, should only ever get 1 result back.
NSError *fetchError;

// Result will be an array with a single AgeGroup entity
NSArray *fetchedObjects = [_managedObjectContext executeFetchRequest:ageGroupRecordRequest error:&fetchError];


// Set up the relationship using the fetched entity
// Crashes when saving if below line is uncommented
newMO.isInAgeGroup = fetchedObjects[0];

My understanding is that basically something else has modified the context, and during the save CoreData has noticed this and stopped. I've read about changing the MergePolicy on the ManagedObjectContext but I don't really want to do this without knowing why I have to.

It's interesting to note that if I comment out the line that attempts to set the relationship it works fine. (except of course the relationship isn't set)

As far as I can see I am passing the managedObjectContext correctly to each view controller. I have also made sure that there are not multiple contexts accessing the same persistent store.

Is it likely to be the FetchedResultsController in the previous View Controller that is modifying the context for some reason?

Is anyone able to offer some information and perhaps a possible solution? I'd rather not have to change the merge policy. I can't see why I should have to considering its a rather simple example. I've been pulling my hair out most of the day on this.

I can't help but think it's most likely something simple I'm missing.

Thanks, Brett.

Upvotes: 0

Views: 3668

Answers (2)

carlos_ms
carlos_ms

Reputation: 838

I had this same issue but the suggested solution wasn't working for me. What worked was to create the context using concurrency type: NSMainQueueConcurrencyType even though I don't need it and I'm not using concurrency at all but somehow copying the preloaded database created the optimistic locking failure. Then wrap the save call in a the context's performBlockAndWait:

To create the context:

[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

To save changes:

__block NSError *error = nil;
__block BOOL savedOK = NO;
[managedObjectContext performBlockAndWait:^{
   savedOK = [managedObjectContext save:&error];
}];

This is even documented in iOS API, look for Concurrency inside NSManagedObjectContext class.

Upvotes: 0

Brett
Brett

Reputation: 21

I was able to figure out the solution.

Turns out that my whole problem was caused by my preloaded database. I have a preloaded database that I copy over in the AppDelegate. Turns out that something was wrong with it.

I found this out by commenting out the lines that copied the database over and instead manually added the preloaded data in the AppDelegate. I was able to add data and also set the relationships.

From there I created a new preloaded database and it works fine.

I also found a great Apple developer example -

https://developer.apple.com/library/ios/samplecode/iPhoneCoreDataRecipes/Introduction/Intro.html

Which also showed me an alternative way of adding new entities. In this case they create the actual entity in the prepareForSegue method and pass just the entity not the entire context.

I changed my app to use that method as well (before I figured out that the database was the issue) but it still crashed. I've decided to keep it that way though as it seems more elegant.

Upvotes: 2

Related Questions