Cannot Delete Object from Core Data Using RestKit 0.20

The code below does not delete the object. The console displays the message "delete was successful" so that the entity is found. All other operations that 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 this removal operation will not be successful and what is the solution for its operation.

+7
source share
4 answers

I found this solution:

In fact, you should retrieve data from persistent storage, not from the currently 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]; 
+5
source

I am doing the same and have almost the same code. In my code I also do the deletion and saved ....

But it is not deleted when I check the DB.

the problem is not in the simulator ... SURE bcz I also get the same problem on the device. there is something called a child context, this could be the reason ... Check out 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 find that pls solution is shared here

+2
source

@Sumitiscreative I came across the same issue today. What if it was discovered that usually using Core Data you should use

 [NSManagedObject save:] 

to save changes. I broke through Restkit a bit and found this

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

Calling this after the delete method described above works to remove the object from the database.

** Edit - I would also just make this comment, but I have no option

+2
source

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

If you need to delete data from the outside, you can use persistentStoreManagedObjectContext and save it after deleting.

In addition, if you want to check at the end whether it is correctly deleted using restkit or not. what can you do...

make a request for deletion, after success, check with the same identifier if the item exists. (for reference only)

+1
source

All Articles