Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discard CoreData changes in one object when RKObjectManager failed to post object

how to discard CoreData changes made to an instance of NSManagedObject when POST/PUT request of this object fails in RKObjectManager?

NSManagedObject *object = ...;
[object setValue:@"test" forKey: @"test"];
[[RKObjectManager sharedManager] postObject:object 
                                 parameters:nil 
                                    success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog(@"success, nothing to do"); }
                                    failure:^(RKObjectRequestOperation *operation, NSError *error) { 
  // I want to discard changes made to object here 
}

UPDATE: RestKit 0.20

like image 481
Alex Kurkin Avatar asked Apr 11 '13 08:04

Alex Kurkin


2 Answers

I know nothing about RKObjectManager, but in order to discard changes to NSManagedObject instance you could call:

[object.managedObjectContext refreshObject:object mergeChanges:NO]

This will discard all changes and turn the object into fault.

see here

like image 186
Dan Shelly Avatar answered Oct 16 '22 13:10

Dan Shelly


Swift solution:

import CoreData

extension NSManagedObject {
    func cancelChanges() {
        managedObjectContext?.refresh(self, mergeChanges: false)
    }
}

Using:

object.cancelChanges()
like image 39
Vasilii Muravev Avatar answered Oct 16 '22 13:10

Vasilii Muravev