pqteru
pqteru

Reputation: 451

Unrecognized selector when deleting a NSManagedObject

I'd like to delete a core data object by fetched the object first, so

in FetchObject.m

- (void) actionDelete {
    AModel *aModel = [[aModel alloc] init];
    AObj *aObj = [aModel readDataWithAttributeName:@"keyword" attributeValue:@"value"];

    [aModel deleteObject:aObj];
}

aObj did fetch and obtain.
in AModel.m

- (void)deleteObject:(AObj *)aObj
{
    [appDelegate.managedObjectContext delete:aObj];
    NSError *error;
    if (![appDelegate.managedObjectContext save:&error]) {
        NSLog(@"Error: %@", [error description]);
    }
}

But, when I test it, here came out an error

-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0xa43ece0

After searching the solution a bit, seems like the target has been release before deleteObject. Is there any way to solve the problem?

Upvotes: 0

Views: 634

Answers (1)

Midhun MP
Midhun MP

Reputation: 107131

The following code is causing the issue:

[appDelegate.managedObjectContext delete:aObj];

Replace it with:

[appDelegate.managedObjectContext deleteObject:aObj];

NSManagedObjectContext doesn't have a delete method, it only has a deleteObject method.

- (void)deleteObject:(NSManagedObject *)object

Parameters

object

A managed object.

Discussion

When changes are committed, object will be removed from the uniquing tables. If object has not yet been saved to a persistent store, it is simply removed from the receiver.

Upvotes: 4

Related Questions