gpt4 book ai didi

ios - 在 NSOperationQueue 和 AFNetworking 中等待操作结束时显示 ActivityView

转载 作者:行者123 更新时间:2023-11-29 04:07:54 26 4
gpt4 key购买 nike

我找到了一种方法来对 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];

}

队列工作完美:应用程序等待解析操作结束,然后调用anotherMethod。但是我需要在仍有加载操作时显示一种 activityView 来广告用户:如你所见,我尝试将其添加到 addOperation 之间waitUntilAllOperationsAreFinished 但我看不到任何东西。这是正确的方法吗?那么,在哪里放置 ActivityView 代码以查看它直到所有操作都完成,或者使用其他方法来实现这一技巧?谢谢!

最佳答案

可以使用此代码

- (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
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON {
// PARSING FAILURE CODE
}];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

UIActivityIndicatorView *tempSpinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[self.view addSubview:tempSpinner];
[tempSpinner startAnimating];

[queue waitUntilAllOperationsAreFinished];

[tempSpinner stopAnimating];
//release tempSpinner if not using arc using [tempSpinner release];
[self anotherMethod];
}

关于ios - 在 NSOperationQueue 和 AFNetworking 中等待操作结束时显示 ActivityView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14898841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com