Kevin Wu
Kevin Wu

Reputation: 1487

Waiting until AWSS3TransferManagerDownloadRequest is finished (iOS, Objective-C)

I am making an iPhone app and using S3 to store images and now I want to download them and display them in my app. The problem is that I do the AWSS3TransferManagerDownloadRequest right before I display the image. Here is the problem: The first time I want to display the image, it doesn't show up, presumably because the download request has not been finished. After that, whenever I rerun my project, the image shows up ok, presumably because it has already been stored locally. So how can I make the image show up right away. Here is the relevant function.

- (void)makeImageWithBucket:(NSString *)bucket withKey:(NSString *)key atLocation:(NSString *)location{
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

// Construct the NSURL for the download location.
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:location];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

// Construct the download request.
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];

downloadRequest.bucket = bucket;
downloadRequest.key = key;
downloadRequest.downloadingFileURL = downloadingFileURL;

// Download the file.
[[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
                                                       withBlock:^id(AWSTask *task) {
                                                           if (task.error){
                                                               if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
                                                                   switch (task.error.code) {
                                                                       case AWSS3TransferManagerErrorCancelled:
                                                                       case AWSS3TransferManagerErrorPaused:
                                                                           break;

                                                                       default:
                                                                           NSLog(@"Error: %@", task.error);
                                                                           break;
                                                                   }
                                                               } else {
                                                                   // Unknown error.
                                                                   NSLog(@"Error: %@", task.error);
                                                               }
                                                           }

                                                           if (task.result) {

                                                               AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;

                                                               //File downloaded successfully.
                                                           }
                                                           return nil;
                                                       }];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
imageView.image = [UIImage imageWithContentsOfFile:downloadingFilePath];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.frame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentSize = CGSizeMake(375,imageView.frame.size.height);
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];}

Upvotes: 1

Views: 542

Answers (1)

Yosuke
Yosuke

Reputation: 3759

You can update the UI from the 'continue with' block. So, if the image you are displaying is nil, you can update it in this section of your code snippet:

if (task.result) {
   AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;

   //File downloaded successfully.
   //Display the downloaded image here.
}

Upvotes: 1

Related Questions