Background: I have a managed entity, Car. I have a RESTful search API which is located on localhost / cars / search. The returned results are Car objects from the server, but I want to save only the one that the user selects. The rest of the cars that I want to drop when they return from the search.
At first I liked everything:
@interface Car : NSManagedObject //<--- managed object @property (nonatomic, strong) NSNumber* year; @property (nonatomic, strong) NSString* make; @property (nonatomic, strong) NSString* model; @end @interface TransientCar : NSObject //<--- regular NSObject! @property (nonatomic, strong) NSNumber* year; @property (nonatomic, strong) NSString* make; @property (nonatomic, strong) NSString* model; @end
I matched the REST API JSON search results with TransientCar objects to display the search results, but did not save them in context. By default, if you map a managed object, RestKit calls it + a factory convenience object to create the object and insert it into the current context (hardcoded in the context of the sharedManager object store, btw!)
It seemed unstable. So now I just use NSMutableDictionary to store the data of the search results until the user looks into the detailed view and does something worth saving the real managed object for:
RKObjectMapping* tempCarMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]]; [tempCarMapping mapKeyPathsToAttributes: @"year", @"year", @"make", @"make", @"model", @"model", nil];
Is this a good practice? Using NSMutableDictionary as a temporary representation until the user does something that requires inserting a new object into the context? I was a fan of using a subclass of the original managed entity to represent the data, but somehow I could mark it as βdon't holdβ or something like that, but every time I do this, I feel like I am struggling with the framework ( and race conditions). I also tried using the scratch / throwaway context by creating a new RKObjectManager and simply clearing its entire context after that, but the RestKit ActiveRecord category + managedObjectContext method will be hard-coded to return:
[[[RKObjectManager sharedManager] objectStore] managedObjectContext];
This type breaks the ability to use a null context for temp / trash data.