Integrating data with a Restkit Core database with NSManagedObjectContext

Over the past weeks I have been studying Restkit (v0.10.0) and the basic data and the possibilities are endless with these great tools. The problem is that I'm a little overwhelmed by how to see the big picture here. And due to the very fast update of Restkit, most of the tutorial / demo code is outdated and no longer works properly.

I managed to get my table view populated with data from my json on a remote server. I also developed how to make remote data master in combination with caching that works now, but I am struggling with NSManagedObjectContext / NSEntityDescription (master data) and how it works with Restkit when using POST commands.

If I understand correctly, the record is created in Core Data (after the comment line // Create a new instance), and after that this data is used to create a POST request so that the record is sent to the server.

This code is used to create a new record on the server, but when the code is executed (I see the record being created on my server), but my table view is not updated accordingly, the table view is not updated, and therefore the new record is first displayed when the application is restarted. Manually updating data from the server also does not help.

Hope someone can give me some pointers or maybe a tutorial with Restkit / core and POST data. Thank!

- (void)createGoalWithName:(NSString *)name andDescription:(NSString *)goalDescription
{
    Goal* goal = [Goal object];
    goal.identifier = 0;
    goal.name = name;
    goal.goalDescription = goalDescription;

    // Create a new instance of the entity managed by the fetched results controller.
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
    [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];

    [self saveContext];

    [[RKObjectManager sharedManager].router routeClass:[Goal class] toResourcePath:@"/api/goals" forMethod:RKRequestMethodPOST];

    [[RKObjectManager sharedManager] postObject:goal delegate:self];

    [self.tableView reloadData];
}

- (void)saveContext {
    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSError *error = nil;
    if (![context save:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. 

     You should not use this function in a shipping application, 
     although it may be useful during development. 

     If it is not possible to recover from the error, 
     display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
+3
source share
3 answers

RestKit + CoreData :

NSString *postUrl = @"/someurl/newelement";    
[ [RKObjectManager sharedManager] loadObjectsAtResourcePath:postUrl usingBlock:^(RKObjectLoader* loader) {
    loader.serializationMIMEType = RKMIMETypeJSON;

    loader.delegate = nil;
    loader.targetObject = nil;
    loader.method= RKRequestMethodPOST; // change to GET, POST, PUT etc
}];
+2

, , , , , . RestKit , .

Apple: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html, , NSManagedObjectContextDidSaveNotification, mergeChangesFromContextDidSaveNotification: , , , RestKit.

, RestKit, , , , . - , :

[self.managedObjectContextForMainThread performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES];

NSManagedObjectContext* managedObjectContextForMainThread , , .

, ( RestKit...)

+1

I am still using the slightly older version of Restkit. But one key element is that a primary key attribute must be defined. So Restkit can keep your local stored objects and server objects in sync.

In your case, when defining mappings for the Goal object, you will do this: targetMapping.primaryKeyAttribute = @ "identifier";

0
source

All Articles