質問

I use GCD to download Images from server and updates the processing on UILabel then print the label to the screen (ex: it will print to the screen : "Downloading: 3/15 Images")

But at the beginning the label is : "Downloading: 0/15 Images". Then when it finish downloading, the label is "Downloading: 15/15 Images".The user cant see the download processing.

What I want is user can see the processing like: "Downloading: 1/15 Images","Downloading: 2/15 Images"."Downloading: 3/15 Images",...,"Downloading: 15/15 Images".

This is my code:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Here your non-main thread.
        NSString *text;

        for (int i = 0;i<[self.pageImages count];i++){
            NSString *image = [self.pageImages objectAtIndex:i];

            [dataManage downloadImagesFromUrl: image ];
            text = [NSString stringWithFormat:@“Downloading %d/%d”,i,self.pageImages.count];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            //Here you returns to main thread.
            [downloadLabel setText:text];
        });
    });
役に立ちましたか?

解決

Move

dispatch_async(dispatch_get_main_queue(), ^{
    //Here you returns to main thread.
    [downloadLabel setText:text];
});

inside the for loop so that the UI is updated after each download (rather than only at the end of all iterations).

他のヒント

Try this code inside the dispatch_async block:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
   [downloadLabel setText:text];
}];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top