Advertisement
gormster

RestKit Fail

May 5th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 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".
  2.  
  3. NSManagedObjectModel* model = [NSManagedObjectModel mergedModelFromBundles:nil];
  4. store = [[RKManagedObjectStore alloc] initWithManagedObjectModel:model];
  5. [store addInMemoryPersistentStore:NULL];
  6. [store createManagedObjectContexts];
  7.  
  8. RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:@"Entity" inManagedObjectStore:store];
  9.  
  10. [mapping addAttributeMappingsFromArray:@[@"required", @"optional"]];
  11.  
  12. RKEntityMapping* childMapping = [RKEntityMapping mappingForEntityForName:@"ChildEntity" inManagedObjectStore:store];
  13.  
  14. [childMapping addAttributeMappingsFromArray:@[@"required", @"optional"]];
  15. childMapping.discardsInvalidObjectsOnInsert = YES;
  16.  
  17. [mapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
  18.  
  19. NSArray* object = @[
  20.                     @{ @"required": @"one",
  21.                        @"optional": @"two",
  22.                        @"children": @[
  23.                                @{@"required": @"four", @"optional": @"three"},
  24.                                @{@"required": @"five", @"optional": @"six"},
  25.                                @{@"optional": @"eight"}
  26.                                ]
  27.                        }
  28.                     ];
  29.  
  30.  
  31. NSDictionary *mappingsDictionary = @{ NSNull.null: mapping };
  32. RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:object mappingsDictionary:mappingsDictionary];
  33. mapper.mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
  34. NSError *mappingError = nil;
  35. BOOL isMapped = [mapper execute:&mappingError];
  36. if (isMapped && !mappingError) {
  37.     // Yay! Mapping finished successfully
  38.     NSError* flub;
  39.     BOOL rslt = [store.mainQueueManagedObjectContext save:&flub];
  40.     NSAssert(rslt == YES, @"Failed to save with error %@",flub);
  41. } else {
  42.     NSLog(@"Mapping error %@",mappingError);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement