Skip to content

Instantly share code, notes, and snippets.

@cbertoldi
Created November 28, 2011 14:58
Show Gist options
  • Select an option

  • Save cbertoldi/1400682 to your computer and use it in GitHub Desktop.

Select an option

Save cbertoldi/1400682 to your computer and use it in GitHub Desktop.
Problem
// in the app delegate
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://192.168.0.29:8080/"];
RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"TestJSON.sqlite"];
[objectManager setObjectStore:objectStore];
// following is called within a view
if (self) {
loginPayload = @"{ \"conti\":[ { \"id\": 1, \"numeroConto\":\"000001\", \"iban\":\"IT0000000000000000000000001\", \"intestatario\":\"ciccio pasticcio\", \"contoDefault\":true }, { \"numeroConto\":\"000002\", \"iban\":\"IT0000000000000000000000002\", \"intestatario\":\"nome e cognome\", \"contoDefault\":false }, { \"numeroConto\":\"000003\", \"iban\":\"IT0000000000000000000000003\", \"intestatario\":\"nome e cognome\", \"contoDefault\":false }, { \"numeroConto\":\"000004\", \"iban\":\"IT0000000000000000000000004\", \"intestatario\":\"nome e cognome\", \"contoDefault\":false }, { \"numeroConto\":\"000005\", \"iban\":\"IT0000000000000000000000005\", \"intestatario\":\"nome e cognome\", \"contoDefault\":false }, { \"numeroConto\":\"000006\", \"iban\":\"IT0000000000000000000000006\", \"intestatario\":\"nome e cognome\", \"contoDefault\":false }] }";
dictionary = [loginPayload objectFromJSONString];
mappingProvider = [RKObjectMappingProvider new];
RKManagedObjectMapping *contoMapping = [RKManagedObjectMapping mappingForClass:[Conto class]];
[contoMapping mapKeyPath:@"numeroConto" toAttribute:@"numeroConto"];
[contoMapping mapKeyPath:@"iban" toAttribute:@"iban"];
[contoMapping mapKeyPath:@"intestatario" toAttribute:@"intestatario"];
[contoMapping mapKeyPath:@"contoDefault" toAttribute:@"contoDefault"];
[mappingProvider setMapping:contoMapping forKeyPath:@"conti"];
RKObjectMapper* mapper = [RKObjectMapper mapperWithObject:dictionary
mappingProvider:mappingProvider];
mapper.targetObject = self->targetObject;
mapper.delegate = self;
RKObjectMappingResult *result = [mapper performMapping];
[[RKObjectManager sharedManager].mappingProvider setMapping:contoMapping
forKeyPath:@"conti"];
NSError *error = nil;
CBAppDelegate *mainDelegate = (CBAppDelegate*)[[UIApplication sharedApplication] delegate];
if ([mainDelegate.managedObjectContext hasChanges]) {
NSLog(@"Something to save"); // never happen
} else {
NSLog(@"Nothing to save");
}
if (![mainDelegate.managedObjectContext save:&error]) {
NSLog(@"Error db sqlite %@", error);
} else {
NSLog(@"Ok!"); // always this, obviously, since there is nothing to save
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment