2

I am trying to delete an entity in core data.

I use the following code:

        //update core data
        [context deleteObject:myEntity];
        [myEntity deleteInContext:context];

        [context save:&error];

So far I know that actually one of the delete lines should do the job, however none of them does. Nothing happens at all. No error message. Nothing happens. context is my NSManagedObjectContext.

Can anyone help me? Thank you!!!!!

7
  • Any hints if you use NSLog(@"Context: %@, Entity: %@", context, myEntity);? Commented Jun 20, 2012 at 13:05
  • thats the output: Context: (null), Entity: <myEntity: 0x80827b0> (entity: myEntity; id: 0x8099240 <x-coredata://F5501461-A354-431B-9679-7AE0987531ED/myEntity/p5> ; data: { done = nil; item = "Tree"; }) Commented Jun 20, 2012 at 13:10
  • 2
    If context is null then that would be the biggest hint you need. Commented Jun 20, 2012 at 13:19
  • In the same code I am editing one attribute of my entity and context is there null as well. But there it works. Commented Jun 20, 2012 at 13:22
  • You're trying to delete a (managed) object, not an entity. Commented Jun 20, 2012 at 13:33

2 Answers 2

4

[context deleteObject:myEntity] is enough to delete an object. [myEntity deleteInContext:context] is redundant.

If you want the changes to be visible at once, do [context processPendingChanges], otherwise the object will be marked as "to be deleted", but the changes will be processed in the end of the runloop cycle.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you! however your suggestion does not solve my problem. my code is now: [context deleteObject:myEntity]; NSLog(@"context is now %@", context); [context processPendingChanges]; [context save:&error];
Can you show log output of myEntity.managedObjectContext as well? Since it seems that your context is nil (which it indeed should not).
0

In consideration of your result from logging the context: (null), the fix has to be to use a valid context.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.