Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Relationships cause save error after delete

Tags:

This question is probably a long shot. I can't figure out the errors I'm getting on my core data project when I save after I delete an entity.

I have two main entities that I work with, an Outfit, and an Article. I can create them with no problem but when I delete them I get the follow error log:

For the Outfit:

2009-09-22 20:17:37.771 itryiton[29027:20b] Operation could not be completed. (Cocoa error 1600.) 2009-09-22 20:17:37.773 itryiton[29027:20b]   {     NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1600.)";     NSValidationErrorKey = outfitArticleViewProperties;     NSValidationErrorObject = <Article: 0x12aa3c0> (entity: Article; id: 0x12b49a0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/Article/p1> ; data: {     articleID = 2009-09-22 19:05:19 -0400;     articleImage = 0x12b4de0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/ArticleImage/p1>;     articleType = nil;     attributeTitles = "(...not nil..)";     color = nil;     comment = nil;     dateCreated = 2009-09-22 19:05:19 -0400;     designer = nil;     imageView = "(...not nil..)";     location = "(...not nil..)";     outfitArticleViewProperties =     (         0x12b50f0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/OutfitArticleViewProperties/p1>     );     ownesOrWants = 0;     pattern = nil;     price = nil;     retailer = nil;     thumbnail = "(...not nil..)";     washRequirements = nil;     wearableSeasons = nil; });     NSValidationErrorValue =     {(         <OutfitArticleViewProperties: 0x1215340> (entity: OutfitArticleViewProperties; id: 0x12b50f0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/OutfitArticleViewProperties/p1> ; data: {     article = 0x12b49a0 <x-coredata://7046DA47-FCE1-4E21-8D7B-E532AAC0CC46/Article/p1>;     articleViewPropertiesID = nil;     outfit = nil;     touch = nil;     view = "(...not nil..)"; })     )}; } 

And if I delete an Article I get:

2009-09-22 18:58:38.591 itryiton[28655:20b] Operation could not be completed. (Cocoa error 1560.) 2009-09-22 18:58:38.593 itryiton[28655:20b]   DetailedError: {     NSLocalizedDescription = "Operation could not be completed. (Cocoa error 1600.)";     NSValidationErrorKey = articleImage;     NSValidationErrorObject = <Article: 0x12aa340> (entity: Article; id: 0x12b3f10 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/Article/p1> ; data: {     articleID = 2009-09-22 18:58:26 -0400;     articleImage = 0x12b4d00 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/ArticleImage/p1>;     articleType = nil;     attributeTitles = "(...not nil..)";     color = nil;     comment = nil;     dateCreated = 2009-09-22 18:58:26 -0400;     designer = nil;     imageView = "(...not nil..)";     location = "(...not nil..)";     outfitArticleViewProperties =     (         0x12b5010 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/OutfitArticleViewProperties/p1>     );     ownesOrWants = 0;     pattern = nil;     price = nil;     retailer = nil;     thumbnail = "(...not nil..)";     washRequirements = nil;     wearableSeasons = nil; });     NSValidationErrorValue = <ArticleImage: 0x12ad600> (entity: ArticleImage; id: 0x12b4d00 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/ArticleImage/p1> ; data: {     article = 0x12b3f10 <x-coredata://05340FA6-B5DC-4646-A5B4-745C828C73C3/Article/p1>;     image = "(...not nil..)"; }); } 

A 1600 error is:

NSValidationRelationshipDeniedDeleteError
Error code to denote some relationship
with delete rule NSDeleteRuleDeny is
non-empty.

Available in Mac OS X v10.4 and later.

Declared in CoreDataErrors.h.

But I can't see for the life of me which relationship would be preventing the delete. If some Core Data wizard can see the error of my ways, I would be humbled.

I can't mark this solved, because I didn't really solve it, but I do have a working work-around. In the .m for each of my managedObjects I added a method that looks like:

-(void) deleteFromManangedObjectContext{     self.outfit = nil;     self.article = nil;     [[self managedObjectContext] deleteObject:self];  }  

So you can see, first I manually nil out the relationships, and then I have the object delete itself. In other objects, instead of nil-ing, my delete method is called on some of the objects relationships, to get a cascade.

like image 816
SooDesuNe Avatar asked Sep 23 '09 00:09

SooDesuNe


People also ask

What is delete rule in Core Data?

A delete rule defines what happens when the record that owns the relationship is deleted. Select the notes relationship of the Category entity and open the Data Model Inspector on the right. By default, the delete rule of a relationship is set to nullify. Core Data supports four delete rules: No Action.

How do I delete all data from Core Data?

One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

How do I save in Core Data?

Right-click on your project's folder in the project navigator and then New File… In the new window, type “data” in the top right corner, select Data Model, and press Next. Give it a name, and save it. Now, let's add all the necessary code to connect Core Data with our project.


1 Answers

I just had the problem of delete fail, and landed on this question. And I've figured out my problem and thought that I'd share that too and maybe someone will have the same problem as well.

The mistake I made is that the object (A) I am trying to delete have a relationship to another object (B) with NULL as delete rule. However, object B also have a relationship to A and it's non-optional. Therefore, when I delete A, B's relationship of A becomes null which is not allowed. When I change the delete rule to cascade and it worked.

like image 62
lionel Avatar answered Nov 02 '22 18:11

lionel