在 SLRequestHandler 中显示 UIAllertView 时系统挂起

System hangs when showing a UIAllertView inside a SLRequestHandler

我正在使用 SLRequest 将用户的视频发送到 Twitter。 post 请求完成后,我想通知用户上传是否成功。但是如果我在 SLRequestHandler 中显示一个 UIAlertView,系统就会挂起并且根本不会显示警报视图。在 SLRequestHandler 中有一个 UIAlertView 是不行的吗?根据 post 请求的结果显示自定义消息的更好方法是什么?

这是我的示例代码:

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];
                      }
                  }];

所有UI相关操作必须在主线程上。

你会尝试在主线程上调度你的警报视图吗?

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];
});

请注意,UIAlertView 自 iOS 8 起已弃用,建议使用 UIAlertViewController。

您正试图在块中显示警报消息。 警报是 UI 线程(主线程)控件。因此,修改 else 部分并在 dispatch_async 中显示您的警报,它将起作用。

dispatch_async(dispatch_get_main_queue(), ^{
  [theAlert show];
});