Anders
Anders

Reputation: 2941

RestKit doesn't save data to database

I'm using RestKit for the first time, and I am having some problems with getting objects to get saved to the database, or their values isn't getting stetted.

I have a JSON request that looks something like this:

{"categories":{"id":1 ... 

I set the mapping for the Category like this:

RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:@"[base-url]"]; 

RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"App.sqlite"];
objectManager.objectStore = objectStore;

objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;

RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectManager.objectStore];

[objectManager.mappingProvider setObjectMapping:categoryMapping forResourcePathPattern:@"[path-to-json].json"];

[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@"categories"];

// Maps keys between the database and costom model object.
categoryMapping.primaryKeyAttribute = @"categoryId";
[categoryMapping mapKeyPath:@"categories.id" toAttribute:@"categoryId"];
[categoryMapping mapKeyPath:@"categories.name" toAttribute:@"name"];
[...]

My custom model object for the category looks like this:

@interface Category : NSManagedObject

@property (nonatomic, retain) NSNumber *categoryId;
@property (nonatomic, retain) NSString *name;
[...]

@implementation Category

@dynamic categoryId;
@dynamic name;
[...]

I load the objects like this (much as of the Twitter example):

// Loads from the database
- (void)loadObjectsFromDataStore {

    NSFetchRequest *request = [Category fetchRequest];
    _categories = [Category objectsWithFetchRequest:request];
}

- (void)loadCategories {
    RKObjectManager *objectManager = [RKObjectManager sharedManager];
    [objectManager loadObjectsAtResourcePath:@"[path-to-json].json" delegate:self];
}

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {

    // The array is always empty.
    NSLog(@"Objects: %@", objects);

    [[NSUserDefaults standardUserDefaults] synchronize];
    [self loadObjectsFromDataStore];
}

And as I said, when I inspect the local database from the simulator it gets inserted but the values is NULL and the category_id is always 0.

Any ideas on how to solve this? Would be very appreciated!

Upvotes: 0

Views: 904

Answers (1)

Paul de Lange
Paul de Lange

Reputation: 10633

Try this mapping:

RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForClass: [Category class]];
categoryMapping.primaryKeyAttribute = @"categoryId";
categoryMapping.rootKeyPath = @"categories";
[categoryMapping mapKeyPathsToAttributes: @"id", @"categoryId", @"name", @"name", nil];

and

[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@""];

It says, use this map when you detect "categories", and index the core data object with by "categoryId".

OR

RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForClass: [Category class]];
categoryMapping.primaryKeyAttribute = @"categoryId";
[categoryMapping mapKeyPathsToAttributes: @"id", @"categoryId", @"name", @"name", nil];

and

[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@"categories"];

Upvotes: 1

Related Questions