0

我找到了一种方法来排队 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。但是当仍然有加载操作时,我需要向用户展示一种活动视图:如你所见,我试图在addOperationwaitUntilAllOperationsAreFinished之间添加它,但我什么也看不到。这是正确的方法吗?那么,在所有操作完成之前放置 activityView 代码以查看它的正确位置在哪里,或者另一种方法来做到这一点?谢谢!

4

1 回答 1

1

可以使用此代码

- (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];
}
于 2013-02-15T16:58:43.533 回答