Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MOM has two entities: Entity and ChildEntity. Both have "required" and "optional" string attributes, which are required and optional, respectively. Entity has a to-many relationship to ChildEntity called "children".
- NSManagedObjectModel* model = [NSManagedObjectModel mergedModelFromBundles:nil];
- store = [[RKManagedObjectStore alloc] initWithManagedObjectModel:model];
- [store addInMemoryPersistentStore:NULL];
- [store createManagedObjectContexts];
- RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:@"Entity" inManagedObjectStore:store];
- [mapping addAttributeMappingsFromArray:@[@"required", @"optional"]];
- RKEntityMapping* childMapping = [RKEntityMapping mappingForEntityForName:@"ChildEntity" inManagedObjectStore:store];
- [childMapping addAttributeMappingsFromArray:@[@"required", @"optional"]];
- childMapping.discardsInvalidObjectsOnInsert = YES;
- [mapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
- NSArray* object = @[
- @{ @"required": @"one",
- @"optional": @"two",
- @"children": @[
- @{@"required": @"four", @"optional": @"three"},
- @{@"required": @"five", @"optional": @"six"},
- @{@"optional": @"eight"}
- ]
- }
- ];
- NSDictionary *mappingsDictionary = @{ NSNull.null: mapping };
- RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:object mappingsDictionary:mappingsDictionary];
- mapper.mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
- NSError *mappingError = nil;
- BOOL isMapped = [mapper execute:&mappingError];
- if (isMapped && !mappingError) {
- // Yay! Mapping finished successfully
- NSError* flub;
- BOOL rslt = [store.mainQueueManagedObjectContext save:&flub];
- NSAssert(rslt == YES, @"Failed to save with error %@",flub);
- } else {
- NSLog(@"Mapping error %@",mappingError);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement