我正在与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;
答案 0 :(得分: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);
};
}];