iOS - 使用RestKit从RESTful服务获取对象

时间:2014-02-12 16:34:28

标签: ios rest ios7 restkit restkit-0.20

我正在尝试从RESTful服务中获取数据。我从一些教程中得到了这段代码,我希望我能做对。我想我只是缺少一些东西。我明白了:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://www....com"]];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;


NSDictionary *parentObjectMapping = @{
                                      @"lat" : @"latitude",
                                      @"long" : @"longitude",
                                      };

RKEntityMapping *cityMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([City class]) inManagedObjectStore:managedObjectStore];
[cityMapping addAttributeMappingsFromDictionary:@{
                                                       @"name" : @"name",
                                                       @"country" : @"country",
                                                       @"population" : @"population",
                                                       }];

[cityMapping addAttributeMappingsFromDictionary:parentObjectMapping];

[manager addResponseDescriptorsFromArray:@[[RKResponseDescriptor responseDescriptorWithMapping:cityMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"results"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
                                           ]];
[manager getObjectsAtPath:@"/api/autocomplete?q=bos"
               parameters:nil
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
}
                  failure:^(RKObjectRequestOperation *operation, NSError *error)
{
}];

我添加了最后几行,因为我觉得我错过了像execute这样的东西。现在,当我运行这个时,我得到错误:

Unable to perform mapping: No `managedObjectContext` assigned. 

我该如何解决这个问题?我做了其他事吗?感谢

1 个答案:

答案 0 :(得分:1)

正如错误已经暗示:您错过了managedObjectContext。使用[managedObjectStore createManagedObjectContexts];(创建商店后的行)添加它