Created
May 17, 2013 18:34
-
-
Save luketheobscure/5601036 to your computer and use it in GitHub Desktop.
RestKit experiment. Don't actually use this code... It took 2 minutes to parse 10K rows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) getPriceInfo{ | |
// Initialize HTTPClient | |
NSURL *baseURL = [NSURL URLWithString:@"http://localhost"]; | |
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; | |
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; | |
// Initialize RestKit | |
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; | |
// Initialize managed object store | |
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; | |
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; | |
objectManager.managedObjectStore = managedObjectStore; | |
[managedObjectStore createPersistentStoreCoordinator]; | |
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"pricing.sqlite"]; | |
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"]; | |
NSError *error; | |
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:seedPath withConfiguration:nil options:nil error:&error]; | |
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error); | |
// Create the managed object contexts | |
[managedObjectStore createManagedObjectContexts]; | |
// Configure a managed object cache to ensure we do not create duplicate objects | |
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext]; | |
// Setup our object mappings | |
RKEntityMapping *priceMapping = [RKEntityMapping mappingForEntityForName:@"Price" inManagedObjectStore:managedObjectStore]; | |
[priceMapping addAttributeMappingsFromDictionary:@{ | |
@"name" : @"name", | |
@"value" : @"value" | |
}]; | |
RKEntityMapping *productMapping = [RKEntityMapping mappingForEntityForName:@"ProductEntry" inManagedObjectStore:managedObjectStore]; | |
[productMapping addAttributeMappingsFromDictionary:@{ | |
@"id" : @"id", | |
@"name" : @"name", | |
@"status" : @"status", | |
@"comment" : @"comment" | |
}]; | |
RKRelationshipMapping* productPriceRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"prices.price" | |
toKeyPath:@"hasPrices" | |
withMapping:priceMapping]; | |
[productMapping addPropertyMapping:productPriceRelationship]; | |
RKEntityMapping *priceBookMapping = [RKEntityMapping mappingForEntityForName:@"PriceBook" inManagedObjectStore:managedObjectStore]; | |
[priceBookMapping addAttributeMappingsFromDictionary:@{ | |
@"name" : @"name", | |
@"created" : @"created", | |
@"lastCompiled" : @"lastCompiled", | |
}]; | |
RKRelationshipMapping* bookProductRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"productEntries.productEntry" | |
toKeyPath:@"hasProductEntrys" | |
withMapping:productMapping]; | |
[priceBookMapping addPropertyMapping:bookProductRelationship]; | |
// Register our mappings with the provider using a response descriptor | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:priceBookMapping | |
pathPattern:@"/home/css/GetPriceInfoDemo.json" | |
keyPath:@"productPricing.priceBooks.priceBook" | |
statusCodes:[NSIndexSet indexSetWithIndex:200]]; | |
[objectManager addResponseDescriptor:responseDescriptor]; | |
[objectManager getObjectsAtPath:@"/home/css/GetPriceInfoDemo.json" parameters:nil | |
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { | |
NSLog(@"operation: %@", operation); | |
NSLog(@"mappingResult: %@", mappingResult); | |
NSError *error; | |
[objectManager.managedObjectStore.mainQueueManagedObjectContext save:&error]; | |
} | |
failure:^(RKObjectRequestOperation *operation, NSError *error) { | |
NSLog(@"Hit error: %@", error); | |
NSLog(@"operation: %@", operation); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment