iosobjective-cuialertviewdurationon-demand-resources

On Remand Resources - Estimated Time (and how to show an alert depending on the download progress)


Is there a way to get the estimated time of On Demand Resources download?

I'd like to show an alert until they are all downloaded.

[alertDownload showCustom:self image:[UIImage imageNamed:@"icon.jpg"] 
                               color:[UIColor blueColor] 
                               title:@"Download..." 
                               subTitle:@"Download in progress" 
                               closeButtonTitle:nil 
                               duration: ODR ETA];

Right now I have

if (request1.progress.fractionCompleted < 1) {
 // code above
}

but the alert will not automatically disappear when the download is completed, it will look at the duration of the alert.


Solution

  • So, also thanks to the help of @trojanfoe, I achieved this way.

    Basically, I'm not setting the alert duration when creating the alert, but I'm updating it depending on the download progress. Until the download finished, I'm repeatedly setting the duration to 20.0f . Then, when the download completed, I'm setting the duration to 1.0f (so the alert will disappear in 1 second).

    NSTimeInterval _alertDuration;
    
    - (void)viewDidLoad {
     [request1 conditionallyBeginAccessingResourcesWithCompletionHandler:^
                                               (BOOL resourcesAvailable) 
      {
        if (resourcesAvailable) {
         // use it
        } else {
           [request1 beginAccessingResourcesWithCompletionHandler:^
                                               (NSError * _Nullable error) 
         {
               if (error == nil) {
                   [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
                       [alertDownload showCustom:self image:[UIImage 
                               imageNamed:@"icon.jpg"] 
                               color:[UIColor blueColor] 
                               title:@"Download..." 
                               subTitle:@"Download in progress" 
                               closeButtonTitle:nil 
                               duration:_alertDuration];
                        }
                    ];
                } else {
                 // handle error
                }
           }];
        }
    }];
    

    .

    - (void)observeValueForKeyPath:(nullable NSString *)keyPath 
                     ofObject:(nullable id)object 
                     change:(nullable NSDictionary *)change 
                     context:(nullable void *)context {
     if((object == request1.progress) && [keyPath 
                     isEqualToString:@"fractionCompleted"]) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
         if(request1.progress.fractionCompleted == 1) {
             _alertDuration = 1.0f;
         } else {
             _alertDuration = 20.0f;
         }
        }];
     }
    }