Alan
Alan

Reputation: 9471

Perform Action after UIAlertView is showing?

I am uploading a picture to a server. I am using a UIAlertView, AlertView1, which asks the user if he/she wants to upload the picture. If Yes, a second alertview, AlertView2 will show with a progress bar and disappear after the upload is complete.

So, once the user clicks on Yes in AlertView1, I call show AlertView2 and call the method [self uploadPhoto]. So the problem comes that even before AlertView2 has time to show up, the CPU intensive uploadPhoto is running and it delays the AlertView2 from showing up for several seconds. It seems like the AlertView2 finally shows a ways through the uploading process.

How do I start the uploading process only once AlertView2 has shown?

Here is the code

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
      //Detectss AlertView1's buttonClick 
      [self.alertView2 show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{

     //Launches when AlertView1 is dismissed
     [self UploadPhoto]
 }

 -(void)uploadPhoto
{
     //CPU/Network Intensive Code to Upload a photo to a server.
}

Upvotes: 0

Views: 288

Answers (3)

Hitesh
Hitesh

Reputation: 21

Call your method [self uploadPhoto] after calling [self.alertView2 show] with a timer using -

[self performSelector:@selector(uploadPhoto) withObject:nil afterDelay:0.3];

It will call your method after a 3 seconds gap after your alert appears.

Upvotes: 0

Roshan
Roshan

Reputation: 1937

Use multi-threading to dispatch uploadPhoto to another thread. That way you won't block the main thread while performing costly network operations.

dispatch_queue_t queue = dispatch_queue_create("upload queue", NULL);
dispatch_async(queue, ^{
    [self uploadPhoto];
    dispatch_async(dispatch_get_main_queue(), ^{
        //dismiss alertview2 here
    });
});

Upvotes: 0

pickwick
pickwick

Reputation: 3154

Set your controller as the delegate of the second alert view, and upload the photo when you receive the didPresentAlertView: message from your second alert.

Upvotes: 3

Related Questions