Search code examples
iosobjective-ccore-datarestkit

RestKit: create an object without saving it


I have an edit "my entity" controller, where I'd like user to make changes in cells and then save it only if user taps the 'save' button. The problem is, that cells are reusable, so i can't be sure that my data is stored in them. That's why I want to save it somewhere, and duplicating all properties from my object to controller doesn't seem to be a good pattern.

Can I create my object using the class I've already implemented with RestKit without saving it to Core Data? I can't find such method. The only one is:

[[MyObject alloc] initWithEntity:<#(NSEntityDescription *)#> insertIntoManagedObjectContext:<#(NSManagedObjectContext *)#>];

What obviously does save into Core Data :/. What is the best way to do it properly?


Solution

  • You can create the object in a another NSManagedObjectContext. For example:

    NSManagedObjectContext *tmpContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    tmpContext.parentContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
    

    Now create the new object in the tmpContext. If you want to rollback the changes you simply call

    [tmpContext rollback];
    

    Or save it:

    [tmpContext save:&error];