سؤال

After my UIViewController segues... inside the viewDidAppear I am making an asynchronous NSURLConnection call to retrieve some data. One of the pieces of data is a URL for an image that I will show the user on this new view.

I pass my method doing the NSURLConnection a block that I want to use as a callback after all the data is returned from the server; I call it in connectionDidFinishLoading.

Here is the block I am sending as my callback for connectionDidFinishLoading to call:

^(BOOL success, NSDictionary *response, NSError *error) {

            if (success) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [loadingImageView startAnimating];
                    [self setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userSubmission.storageUri]]]];
                });

                NSLog(@"Is Main Thread: %ld", (long)[NSThread isMainThread]);
                userSubmission = [[Submission alloc] initWithDictionary:response];
                if (userSubmission.votes) {
                    _numberOfVotesSubmissions.text = [NSString stringWithFormat:@"%ld", (long)userSubmission.votes];
                } else {
                    _numberOfVotesSubmissions.text = @"0";
                }
                _votesOrSubmissionsImage.image = [UIImage imageNamed:@"list_votes_icon"];


            } else {
                _numberOfVotesSubmissions.text = [NSString stringWithFormat:@"%ld", (long) _clue.submissions];
                _votesOrSubmissionsImage.image = [UIImage imageNamed:@"list_photo_icon"];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [loadingImageView stopAnimating];
            });
        }];

I put a break point at the point where I try to load the image and at the point where I try to stop the "loadingImageView" from animating and I NEVER see the UIActivityViewController.

I have a UIImageView that is 300x300 and I place my UIActivityViewController in the middle of it (via addSubView).

This work beautifully if I start the UIActivityViewController spinning inside of viewDidAppear, however, there's not always going to be a scenario where I'll have an image to show and in that case the default "no image found" image is what I want to present.

I assume my problem is that I am running the NSURLConnection call and the run loop wants to finish my block callback before it starts the other stuff.

Any help is greatly appreciated.

Thanks.

هل كانت مفيدة؟

المحلول

The problem is you are loading the image data on the main thread - that's bad. You also try to do a lot of UI work in the background. Try this:

^(BOOL success, NSDictionary *response, NSError *error) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [loadingImageView startAnimating];
            });

            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:userSubmission.storageUri]];
            NSLog(@"Is Main Thread: %ld", (long)[NSThread isMainThread]);
            userSubmission = [[Submission alloc] initWithDictionary:response];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self setImage:[UIImage imageWithData:imageData]];
                if (userSubmission.votes) {
                    _numberOfVotesSubmissions.text = [NSString stringWithFormat:@"%ld", (long)userSubmission.votes];
                } else {
                    _numberOfVotesSubmissions.text = @"0";
                }
                _votesOrSubmissionsImage.image = [UIImage imageNamed:@"list_votes_icon"];
                [loadingImageView stopAnimating];
            });
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                _numberOfVotesSubmissions.text = [NSString stringWithFormat:@"%ld", (long) _clue.submissions];
                _votesOrSubmissionsImage.image = [UIImage imageNamed:@"list_photo_icon"];
            });
        }
    }];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top