iosobjective-cuialertviewslrequest

System hangs when showing a UIAllertView inside a SLRequestHandler


I am using SLRequest to send user's video to twitter. After finishing the post request, I want to inform the user whether the upload is successful. But if I show a UIAlertView inside the SLRequestHandler, the system simply hangs and doesn't show the alert view at all. Is it a no-go to have a UIAlertView inside the SLRequestHandler? What is the better way to show a custom message based on the result of the post request?

Here is my sample code:

SLRequest *postRequest2 = [SLRequest
                                            requestForServiceType:SLServiceTypeTwitter
                                            requestMethod:SLRequestMethodPOST
                                            URL:requestURL2 parameters:message2];
                 postRequest2.account = twitterAccount;

                 [postRequest2
                  performRequestWithHandler:^(NSData *responseData,
                                              NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      if (error != nil) {
                          NSLog(@"error: %@", error);
                      }
                      else {
                          UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Success!"
                                                                             message:@"Your video is now available in your Twitter account"
                                                                            delegate:nil
                                                                   cancelButtonTitle:@"OK"
                                                                   otherButtonTitles:nil];
                          [theAlert show];
                      }
                  }];

Solution

  • All UI related operations must be on the main thread.

    Would you try to dispatch on the main thread your alert view?

    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Your video is now available in your Twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [theAlert show];
    });
    

    Please note that UIAlertView is deprecated since iOS 8, and the use of UIAlertViewController is recommended.