我正在使用RestKit(0.20)/ JSON从Web服务获取数据。到目前为止,我已经为少量数据工作了,但我需要这项工作用于10,000多条记录。因为获取数据可能需要相当长的时间(特别是通过3G或更慢的连接)我想把它切成碎片但是我遇到线程问题。我想:
到目前为止我的代码是:
- (void) loadPeople {
.. not so relevant code and setup ..
peoplePath = [NSString stringWithFormat:@"http://mywebservice.com/api/v2/people?columns=id,first_name,last_name&sort=updated_at&limit=1000&offeset=%d",offset];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path: peoplePath parameters:nil];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[Person savePeople:mappingResult.array];
[self updatePeopleProgressBar];
} failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog (@"Error: %@", error);
NSLog (@"Response: %@", operation.HTTPRequestOperation.responseString);
}];
[operation start];
}
应该一次又一次地执行从peoplePath开始的部分(将偏移量变量增加1000),但我对实现循环的位置一无所知。 但是,它应该只在当前保存和更新完成后执行下一次迭代:我正在跟踪已保存的数量,以防用户退出应用程序,以便下次我可以恢复。所以我需要某种顺序线程......
我想问题在于我对块和多线程的熟悉知识。谁能给我一个如何做到这一点的线索。
更新方法:
- (void) loadPeople : (NSInteger) offset {
NSString *apiKey = self.APIKey;
NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKMapping *mapping = [Mappings peopleMapping];
NSString *pathPattern = [NSString stringWithFormat:@"%@/people",basePathPattern];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodAny
pathPattern:pathPattern
keyPath:keyPath
statusCodes:statusCodeSet];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/%@",[self clientURLPart],basePathPattern]];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:url];
[[[RKObjectManager sharedManager] HTTPClient] setDefaultHeader:@"X-API-KEY" value:apiKey];
[[[RKObjectManager sharedManager] HTTPClient] setDefaultHeader:@"Content-type application" value:@"json" ];
[[[RKObjectManager sharedManager] HTTPClient] setDefaultHeader:@"Accept application" value:@"json"];
NSString *peoplePath = [NSString stringWithFormat:@"%@/%@?columns=id,first_name,last_name&sort=updated_at&limit=4&offset=%d",[self clientURLPart],basePathPattern,offset];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:peoplePath parameters:nil];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
[Person savePeople:mappingResult.array];
NSLog(@"Foundrecords %d",[mappingResult.array count]);
if ([mappingResult.array count] == 4) {
[self loadPeople:(offset + 4)];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog (@"Error: %@", error);
NSLog (@"Response: %@", operation.HTTPRequestOperation.responseString);
}];
[operation start];
}
答案 0 :(得分:0)
您显示的代码应该是一个采用页码或偏移号的方法。然后,在成功块中,您可以调用方法(递归类型)以使用来自“当前”下载的适当信息开始下一次下载。这可以避免所有与线程相关的问题。