Best practice for temporary objects in RestKit with master data

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.

+6
source share
3 answers

First, I did this in the past, using your method of having two copies of the model, one of which relates to Core Data, and the other is transitional (just NSObject). This worked without any problems for me.

As for your other attempts, I don't think the library forces your hand as much as you think. Check out the API on RKManagedObjectStore and NSManagedObject+ActiveRecord . In particular, RKManagedObjectStore has the managedObjectContext property, the method - (NSManagedObjectContext*)newManagedObjectContext and several methods for merging changes.

You are correct that [NSManagedObject managedObjectContext] always returns the sharedManager context - but it makes sense, it is a class method. Otherwise, how does the class know which context should return? But this is controversial, as there are so many other ways to create new contexts and access to them. Or to get around this completely, you can simply save the link to your temporary context and use it directly.

This gives you several options: to have several ObjectManagers, to have one object manager, but create a temporary context from it and only save the necessary objects, create a transition object based on the managed object.

The NSMutableDictionary option does not seem as flexible as other methods, but I would not say that this is "bad practice."

+3
source

Unfortunately, I don’t have enough StackOverflow reputation to place this answer where it belongs (as comments to other answers), but I wanted to add a few points.

I believe Evan Cordell's answer is wrong. The current version of restkit (0.10.x) does not allow creating contexts for RKManagedObjectLoaders to use, and RKObjectManagers can take storage, but it must be of type RKManagedObjectStore, which is explicitly bound to sqllite. The dev version of restkit (0.20) seems to relax this, so you can store the data in the database in memory. I tried to override the RKManagedObjectStore methods to use the context you provided, but that didn't work ... anyway, the fix seems nontrivial.

Another link, The best approach to create a Temp object for master data with Restkit seems to be to publish the object and get the same object in the response. This is a different problem than was posed in this matter.

Until v.0.20.x is released, which we hope will be soon, it seems that the only alternative is a hierarchy of parallel classes. If I am wrong, I welcome a correction on this!

+5
source

You can also see this answer: Best approach for creating a Temp object for master data using Restkit

This will avoid the problem of the transition object mentioned in the answer of Evan Cordell, who is the main contributor to RestKit.

+2
source

Source: https://habr.com/ru/post/911793/


All Articles