7

The code below does not delete the entity. The "delete was successful" message appears on the console so the entity is found. All other operations I use succeed.

I am using RestKit 0.20.

NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSError *error = nil;

NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:@"Auction" inManagedObjectContext:context]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"AuctionID = %d", auctionID];
[fetchRequest setPredicate:predicate];

NSArray *result = [context executeFetchRequest:fetchRequest error:&error];
if(result.count) {
    Auction *block = result[0];
    [context deleteObject:block];
    BOOL status = [context save:&error];
    if (status == NO) {
        NSLog(@"delete falied for AuctionID:%d, error: %@", auctionID, error);
    }
    else {
        [context processPendingChanges];
        NSLog(@"delete was successful for AuctionID:%d", auctionID);

    }
}

Why might this delete operation not succeed and what is the solution to making it work.

David
  • 9,635
  • 5
  • 62
  • 68
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Where are you seeing the instance after you delete it? – Tom Harrington Feb 26 '13 at 19:22
  • I am using a 3rd party tool "Base" on the *.sqlite file in the simulator. I can delete entities with the tool. – zaph Feb 26 '13 at 22:08
  • I was actually wondering what the symptom of the problem is, and when you see it occur. You said you're having problem deleting data so, when are you seeing the items that you thought you had deleted? – Tom Harrington Feb 26 '13 at 22:31
  • They are not deleting,what more can I say? The data is there before the app starts, still there when the app deletes. I have deleted the *sqlite file, added the entries back with the same program and they won't delete. What I am wondering is if there is something about RestKit 0.2 that is causing some kind of lock? I am running in the 6.1 simulator. – zaph Feb 26 '13 at 23:17
  • I found a solution, see my answer. – Sebastien Mar 14 '13 at 08:42

4 Answers4

5

I found this solution :

In fact, you have to fetch datas from the persistentstore and not the current created managed context :

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyModel"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"id" ascending:NO];
fetchRequest.sortDescriptors = @[descriptor];

// Setup fetched results
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                        managedObjectContext:[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];

// AND TO DELETE A MODEL :

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:myobject];
Sebastien
  • 6,640
  • 14
  • 57
  • 105
  • Thank you :) If you find a better solution, do not hesitate to purpose it – Sebastien Mar 15 '13 at 14:43
  • This does resolve the problem. The issue was the context, `[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext` was the exact fix. – zaph Mar 18 '13 at 18:49
2

i am doing the same thing and have nearly same code. In my code also, i get delete done and saved....

But, its not deleted when i am checking DB.

the problem is not with simulator... SURE bcz i am getting same problem on device also. there is something called child context, it might be the cause...Check these links http://restkit.org/api/0.20.0-dev/Classes/RKManagedObjectRequestOperation.html#//api/name/managedObjectContext RestKit 0.20 — What is the preferred way to create a new NSManagedObject? . If you found solution pls share here

Community
  • 1
  • 1
Sumitiscreative
  • 647
  • 3
  • 10
  • 24
  • Make sure, you guys are checking Database after stopping the app. Because changes are not reflected unless it is in connection. So, Delete -> disconnect and check. In, my case it was still not deleted after checking it like this. And Yes, now i am able to delete it explicitly but thats wrong, it should be deleted by restkit only. If any of you get solution pls post here – Sumitiscreative Mar 14 '13 at 06:01
2

@Sumitiscreative I ran into the same issue today. What if found was that normally using Core Data you have to use

[NSManagedObject save:] 

for it to store the changes. I dug through Restkit a bit and found this

[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext deleteObject:(NSManagedObject *)];
[[RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext saveToPersistantStore:(NSError *)];

Calling this after the above delete method is working to remove the object out of the DB.

**Edit - Also I would have just made this a comment but i don't have the option

Lbatson
  • 1,017
  • 8
  • 18
1

@Lance Hey, pls update your restkit with the latest version. As now, this works in the latest version , if your server related configuration is correct. and if you get success codes for your delete request from server. Then, restkit automatically deletes the data.

If you need to delete any data externally then, you can use persistentStoreManagedObjectContext and after deleting, save it.

Also, if you want to check at your end that whether its correctly deleting via restkit or not. what you can do is ...

make delete request, after success check with same id, if item exists. (just for help)

Sumitiscreative
  • 647
  • 3
  • 10
  • 24