我正在使用AFNetworking库检索JSON数据,并希望添加一个完成块,如下所示:
-(void)getData{
NSString *link=@"http://localhost:8080/address/address/address/";
NSURL *url= [ NSURL URLWithString:link];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
arrayofA=[JSON valueForKeyPath:@"a"];
arrayofB=[JSON valueForKeyPath:@"b"];
[[self techTableView] reloadData];
} failure:nil];
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
//-- This block gets invoked periodically as the download progress
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(totalBytesRead * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
dispatch_async(dispatch_get_main_queue(), ^{
// HUD treatment
});
});
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Completed:");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation start];
}当我添加完成框时,我已经没有结果了,屏幕上什么也没有显示,并且注意到AFJSONRequestOperation块没有被执行,我在那里打印了一个日志,它没有显示出来。有什么问题吗?非常感谢。
发布于 2013-08-24 13:29:32
扔掉这段代码:
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Completed:");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];相反,将您的日志记录放到JSONRequestOperationWithRequest:success:failure:上的块中(在这里,您目前有一个成功块,但没有失败块,并且您应该删除的代码正在删除成功块)。
在setDownloadProgressBlock中,只需推到主线程并更新UI即可。不要费心制造拖延,这只会导致明显的随机行为。另外,不要尝试使用下载的结果,这应该只在success块中完成。
https://stackoverflow.com/questions/18417463
复制相似问题