문제

I am attempting to use MBProgressHUD to pop up a loading animation while the application loads more json data. Here is the code that I use to call the function with MBProgressHUD included.

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
float bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;
if (bottomEdge >= scrollView.contentSize.height) {
    pageCounter++;


    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        // Do something...
        [self loadAdditionalJson];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });

    }
}

And here is the function that is called:

-(void)loadAdditionalJson

{

NSLog(@"%i", pageCounter);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:
                                   @"http://ragetracks.com/?json=1&count=50&page=%i&custom_fields=PostThumb&include=title,content,attachments",
                                   pageCounter]];


NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

    for (int x = 0; x < 50; x++) {
        //NSLog(@"%i", pageCounter*50 -50 + x);
        if ([JSON[@"posts"][x][@"attachments"] count] == 0)
        {
            NSString *temp = [NSURL URLWithString:@"nothing"];
            [imageUrls addObject:temp];
        }
        else
        {
            NSString *temp1;
            //temp1 = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"id"]];
            //NSLog(temp1);
            NSURL *imageUrl =  [NSURL URLWithString:
                                [NSString stringWithFormat:@"%@",
                                JSON[@"posts"][x][@"attachments"][0][@"images"][@"medium"][@"url"]]];
           NSString *title = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"title"]];

            NSString *label = [NSString stringWithFormat:@"%@", JSON[@"posts"][x][@"content"]];

            NSScanner *scanner = [NSScanner scannerWithString:label];
            [scanner scanUpToString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanString:@"%2Ftracks%2F" intoString:nil];
            [scanner scanUpToString:@"&" intoString:&temp1];

            NSURL *temp2 = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.soundcloud.com/tracks/%@.json?client_id=7622aa84a50c9f7609e2f7ed8bc85e81", temp1]];

            if (imageUrl) {
                [imageUrls addObject:imageUrl];
            }
            else{
                NSString *temp = [NSURL URLWithString:@"nothing"];
                [imageUrls addObject:temp];
            }
            [titles addObject:title];
            [contentUrls addObject:temp2];

        }
    }
    [self.collectionView reloadData];

} failure:nil];


    [operation start];
}

The loading animation only appears and disappears too quickly and does not reflect how long it is taking for the application to load the new information. I understand that it has something to do with the main thread, but I am not sure exactly what to do. I have tried turning off AFNetworkActivityIndicatorManager but that does not seem to affect anything. Sleeping the system delays it, but then still does not reflect the loading time properly since after sleep, it comes back and finishes up the loading.

도움이 되었습니까?

해결책

Well, you're asking it to... You delay for a very short time before starting the JSON load (which would seem to be pointless) and then dismiss the progress indicator immediately after starting the load.

I think your confusion is that you think the JSON load is done synchronously - but it isn't.

So, remove:

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

Because it isn't helping you. Just start the load after you start the HUD animation. Then, in the success block (on JSONRequestOperationWithRequest) where you process the JSON, after the scanning code, add the:

[MBProgressHUD hideHUDForView:self.view animated:YES];

If should go right after [self.collectionView reloadData];.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top