RestKit - 从另一个RKObjectManager更新现有对象

时间:2014-09-21 10:35:05

标签: ios objective-c restkit restkit-0.20

我有2个RKObjectManager配置为使用相同的managedObjectStore。

使用第一个RKObjectManager我获取对象列表,使用第二个RKObjectManager我想向这些对象添加数据。

但是,不是更新现有对象,而是创建新对象。

我错过了什么?

这是我的代码:

RKEntityMapping* serverAppMapping = [RKEntityMapping mappingForEntityForName:@"COApp" inManagedObjectStore:[RKObjectManager coopsiManager].managedObjectStore];
[serverAppMapping addAttributeMappingsFromDictionary:@{
                                                    @"id": @"coID"
                                                    }];
serverAppMapping.identificationAttributes = @[ @"coID" ];



RKResponseDescriptor *serverAppResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:serverAppMapping method:RKRequestMethodGET pathPattern:@"/me/apps" keyPath:@"data.items" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKEntityMapping *appsMapping = [RKEntityMapping mappingForEntityForName:@"COApp" inManagedObjectStore:[RKObjectManager coopsiManager].managedObjectStore];
[appsMapping addAttributeMappingsFromDictionary:@{
                                                  @"trackName": @"name",
                                                  @"trackId": @"coID",
                                                  @"artworkUrl60":@"imageURL"}];
appsMapping.identificationAttributes = @[ @"coID" ];

[serverAppMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"app"
                                                                              toKeyPath:@"app"
                                                                            withMapping:appsMapping]];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *appResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:appsMapping method:RKRequestMethodGET pathPattern:@"/th/lookup" keyPath:@"results" statusCodes:[NSIndexSet indexSetWithIndex:200]];

[[RKObjectManager appleManager] addResponseDescriptor:appResponseDescriptor];
[[RKObjectManager coopsiManager] addResponseDescriptor:serverAppResponseDescriptor];


NSString* access_token = [[COAuthManager sharedInstance] getUserAccessToken];

NSDictionary *queryParams;
queryParams = [NSDictionary dictionaryWithObjectsAndKeys:access_token, @"access_token", nil];

[[RKObjectManager coopsiManager] getObjectsAtPath:@"/me/apps" parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSOrderedSet* set = [NSOrderedSet orderedSetWithArray:mappingResult.array];
    [me addNewApps:set];
    self.objects = [me.newApps mutableCopy];
    [self.delegate dataRefrashed];

    NSManagedObjectContext *moc = [[RKObjectManager coopsiManager]managedObjectStore].persistentStoreManagedObjectContext;
    NSError *error;
    if (![moc save:&error]) {
        NSLog(@"Fail");
    }else{
        NSLog(@"luda rest kit objetcs - %@",me.newApps);
    }

    for (COApp * app in self.objects) {

        NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:app.coID, @"id", nil];

        [[RKObjectManager appleManager] getObjectsAtPath:[NSString stringWithFormat:@"/th/lookup"] parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

// app.app = mappingResult.array [0];                 [self.delegate dataRefrashed];             } failure:^(RKObjectRequestOperation * operation,NSError * error){                 [self presentError:错误];             }];         }     } failure:^(RKObjectRequestOperation * operation,NSError * error){         [self presentError:错误];     }];

0 个答案:

没有答案