Deleteing a managed object from an entity
I've got a managedObjectContext with 2 entities, each of which contains multiple entries. Objects of Entity A are represented in a Table View, and I want the user to be able t开发者_如何学运维o delete any entity is Entity A. My problem is that, when I send the request to delete that entry, the wrong entity is being called!
FYI, I’m handling the deleting process in a separate method, so when the delete button in Table view is triggered, before it’s been taken out of the view, I first want to make sure it’s been removed from the managedObjectContext.
- (BOOL) deleteCompletedSuccessfully : (EntityA *) anEntry
{
[self.managedObjectContext deleteObject: anEntry];
NSError *error = nil;
If (![self.managedObjectContext save:&error])
{
NSLog (@”%@”, [error userInfo]);
return NO;
}
return YES;
}
The error is:
Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)….
And the rest of the error message indicates that I’m trying to delete a nil object in EntityB!!!! While anEntry is in fact in EntityA.
I tried encapsulating the input (anEntry is this case) into an array, i.e:
- (BOOL) deleteCompletedSuccessfully : (NSArray *) array
{
EntryA *anEntry = [array objectAtIndex: 0];
// and the rest of the code
The same error. How can I make it look for that particular entry in a particular entity?!
Any help?
I think you've got some conceptual confusion going on here between entities and managed objects.
Entities are abstractions analogous to Classes. Managed objects are actual individual instances of NSManagedObject or one of its subclasses. Entities in the data model tell the managed object context what attributes and relationships to one another the managed object instances will have.
Entities exist solely in the data model whereas Managed objects are in the "object graph" which is the actual functioning group of related objects alive in memory. Entities simply describe to the managed object context how everything fits together much as a class definition tells the compiler how all the properties and behaviors of fit together in a class. Managed objects as instances have data and behaviors just like all other live objects.
Likewise, a managed object context does not add, remove or set the values of entities in any way. Instead, it adds, removes or set the values of managed objects configured by the entities in it's data model.
So, when you say:
I've got a managedObjectContext with 2 entities, each of which contains multiple entities.
What you really mean is:
I've got a data model with 2 entities and a managed object context with many managed objects configured by those entities.
A tableview may display only the data from the instances configured to one entity (that is most common) but the actual data and the insertions and deletion happen to managed object instances and not the entities which are unalterable by that point.
However, I don't think the terminology confusion is the actual cause of your problem. Instead, I think the error is trying to tell you that you are deleting an object configured by EntityA from a required relationship with a an object configured with entityB.
The cocoa error 1570 is a NSValidationMissingMandatoryPropertyError which as the name suggest occurs when you try to save a managed object that has a required property with a nil value. The manage object context tries to validate the object graph before it saves and when it finds a missing required property it throws that error.
I can't tell you anything more because I have no idea what your data model looks like.
精彩评论