Search code examples
ioscore-datarestkitmagicalrecord

RestKit+CoreData+MagicalRecord: how not to persist managed objects


I have a webservice which returns a list of objects (hundreds of products) which don't need to be persisted but just temporary displayed.

The user can select any of them and such items must be saved into the database.

Is it possible to manage this scenario with just one NSManagedObject entity?

Using this respose descriptor:

- (id)productsResultResponseDescriptor {

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Product class]];
    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"products" toKeyPath:@"products" withMapping:[Product mapping]]];  
    ...
    return productsResultResponseDescriptor;
}

+ (RKObjectMapping *)mapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
    [mapping addAttributeMappingsFromDictionary:@{
                                   @"name":@"name",
                                   @"category":@"category"
                                }];
    return mapping;
}

With this configuration I get a bad access exception in the validateValue method of RKMappingOperation.

Could it work or am I supposed to have two different objects

Product : NSObject
ManagedProduct : NSManagedObject

and use the former for data retrieve with RestKit and the latter for saving the chosen products with MagicalRecord?

Many thanks, DAN


Solution

  • Just save them all. Core data will make it much easier for you to load only the required data for display when you have significantly more data than can fit on one screen. Then, just add a flag to your entity which is set (and saved) when the user selects or deselects a product.

    Note that you could do it using a plain and a managed object but it's more code and complexity for you and it's less efficient both in terms of memory and processor usage.