2

我正在使用 Restkit 同时发送 2 个请求,并且我收到了来自它们的响应,但只有一个请求接收到任何对象。如果我将它们一一发送,那么我的 objectloader 会收到所有对象。

第一个请求:

self.firstObjectManager = [RKObjectManager sharedManager];
[self.firstObjectManager loadObjectsAtResourcePath:[NSString stringWithFormat: @"/%@.json", subUrl] usingBlock:^(RKObjectLoader *loader){
    [loader.mappingProvider setObjectMapping:designArrayMapping forKeyPath:@""];
    loader.userData = @"design";
    loader.delegate = self;
    [loader sendAsynchronously];
}];

第二个请求:

self.secondObjectManager = [RKObjectManager sharedManager];
[self.secondObjectManager loadObjectsAtResourcePath:[NSString stringWithFormat: @"/%@.json", subUrl] usingBlock:^(RKObjectLoader *loader){
    [loader.mappingProvider setObjectMapping:designerMapping forKeyPath:@""];
    loader.userData = @"designer";
    loader.delegate = self;
    [loader sendAsynchronously];
}];

我的对象加载器:

-(void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
    //NSLog(@"This happened: %@", objectLoader.userData);
    if (objectLoader.userData == @"design") {
        NSLog(@"Design happened: %i", objects.count);
    }else if(objectLoader.userData == @"designer"){
        NSLog(@"designer: %@", [objects objectAtIndex:0]);
    }

}

我的回复:

2012-11-18 14:36:19.607 RestKitTest5[14220:c07] Started loading of request: designer
2012-11-18 14:36:22.575 RestKitTest5[14220:c07] I restkit.network:RKRequest.m:680:-[RKRequest updateInternalCacheDate] Updating cache date for request <RKObjectLoader: 0x95c3160> to 2012-11-18 19:36:22 +0000
2012-11-18 14:36:22.576 RestKitTest5[14220:c07] response code: 200
2012-11-18 14:36:22.584 RestKitTest5[14220:c07] Design happened: 0
2012-11-18 14:36:22.603 RestKitTest5[14220:c07] I restkit.network:RKRequest.m:680:-[RKRequest updateInternalCacheDate] Updating cache date for request <RKObjectLoader: 0xa589b50> to 2012-11-18 19:36:22 +0000
2012-11-18 14:36:22.605 RestKitTest5[14220:c07] response code: 200
2012-11-18 14:36:22.606 RestKitTest5[14220:c07] designer: <DesignerData: 0xa269fc0>

更新:设置我的基本网址

RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://iphone.meer.li"];
[RKObjectManager setSharedManager:[RKObjectManager managerWithBaseURL:baseURL]];

解决方案

问题是我对两个对象管理器都使用了共享管理器,所以我最终做了:

RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://iphone.meer.li"];
RKObjectManager *myObjectManager = [[RKObjectManager alloc] initWithBaseURL:baseURL];
self.firstObjectManager = myObjectManager;

和:

RKURL *baseURL = [RKURL URLWithBaseURLString:@"http://iphone.meer.li"];
RKObjectManager *myObjectManager = [[RKObjectManager alloc] initWithBaseURL:baseURL];
self.secondObjectManager = myObjectManager;
4

1 回答 1

2

使用块语法进行加载,

self.secondObjectManager = [RKObjectManager sharedManager];
[self.secondObjectManager loadObjectsAtResourcePath:[NSString stringWithFormat: @"/%@.json", subUrl] usingBlock:^(RKObjectLoader *loader){
    [loader.mappingProvider setObjectMapping:designerMapping forKeyPath:@""];
    loader.userData = @"designer";
     loader.onDidLoadObjects = ^(NSArray *objects){
         NSLog(@"Loaded objects %@", objects);
      };
     loader.onDidFailedWithError = ^(NSError *error){
        NSLog(@"Failed with error %@", error.userInfo);
   }
}];

编辑

使用您自己的对象管理器并保留它,然后以这种方式使用它,

不要使用 sharedManager 而不是 alloc/init 它并提供它 objectstore 并查看。希望这对你有帮助,

[self.objectManager loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader){
        [loader.mappingProvider setMapping:[MyClass mapping] forKeyPath:@""];
        loader.onDidLoadObjects = ^(NSArray *objects){

        };
        loader.onDidLoadResponse = ^(RKResponse *response){
            NSLog(@"Loaded response %@", response.bodyAsString);
        };
        loader.onDidFailLoadWithError = ^(NSError *error){
            NSLog(@"Error occurred ... %@", error.userInfo);

        };
    }];
于 2012-11-18T21:21:56.743 回答