7
votes

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.

4
Where are you seeing the instance after you delete it?Tom Harrington
I am using a 3rd party tool "Base" on the *.sqlite file in the simulator. I can delete entities with the tool.zaph
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
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
I found a solution, see my answer.Sebastien

4 Answers

5
votes

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];
2
votes

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

2
votes

@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

1
votes

@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)