我找到了一种方法来排队 JSON 解析操作以等待完整的数据解析,这是代码:
- (void)LoadParse { // this method is called by a UIButton
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// PARSING SUCCESS CODE
NSLog(@"operation completed"); <--- END OPERATION
[self anotherMethod]; <--- CALL METHOD AFTER OPERATION IS FINISHED
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON {
// PARSING FAILURE CODE
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation]; <--- START OPERATION
// I NEED TO SHOW A SORT OF INDICATOR TO ADVERT USER THAT OPERATION ARE STILL LOADING
// EXAMPLE: [DejalBezelActivityView activityViewForView:self.view withLabel:@"LOADING..."].showNetworkActivityIndicator = YES;
[queue waitUntilAllOperationsAreFinished];
}
队列完美运行:app 等待解析操作结束,然后调用anotherMethod。但是当仍然有加载操作时,我需要向用户展示一种活动视图:如你所见,我试图在addOperation和waitUntilAllOperationsAreFinished之间添加它,但我什么也看不到。这是正确的方法吗?那么,在所有操作完成之前放置 activityView 代码以查看它的正确位置在哪里,或者另一种方法来做到这一点?谢谢!