开发者

Save loop deleting one end of many-to-many CoreData relationship

I've been looking at this one for a while but can't seem to crack it. Its the first many-to-many relationship I have built in CoreData and obviously there is something simple I am missing...

I have a screen where I maintain two different Core Data 'entities', the entities are related via a many-to-many relationship. The first entity and the relationships between the first and second entity are maintained on the first tab of the screen and this seems to be working fine allowing me to delete the first entity instances and add/remove relationships between the first and second entities.

My problems are on the second tab where I maintain the second entity. They entities display fine and I can update their attributes, however, attempts to delete them results in a never-ending save loop.

I believe I have verified the loop by adding an NSLog entry into the willSave method of the second entity's NSManagedObject class.

The entities are displayed in NSTableViews backed by NSArrays (I haven't used binding). I'm sharing the one managedObjectContext between the two tabs and the delete operation is simple (see below):

int currentRow = [[tableView selectedRowIndexes] firstIndex];
NSManagedObject *targetObject = [self.array objectAtIndex:currentRow];
N开发者_JAVA百科SError *error = nil;
[managedObjectContext deleteObject:targetObject];
if (![managedObjectContext save:&error]) {
   NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
   abort();
}

Hopefully someone can help...

Regards

Scott


OK, so it ended up being simple (and my fault :)

I should have mentioned that the data I had been using had been loaded into the generated SQLite3 file using a separate data load script (I'm using data from a variety of sources).

There were some records I had added that did not contain a value for Z_OPT and CoreData was failing on delete of these records (then re-attempting the delete over and over).

Once I populated Z_OPT everything worked. I think I'll be letting CoreData manage all changes to the database from now on :)

Thanks again to @TechZen and @ImHuntingWabbits for your responses.


Not a lot to go on here but check your delete rules.

If you have a many-to-many relationship set to delete on both sides, deleting one object can set off a cascade of deletes because deleting one object triggers the deletion of all its related objects which in turn delete all their related objects and so on.

If you've customized willSave or willTurnIntoFault or similar methods you may have introduced a loop in that code as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜