我有一组 UIActivities,我将数据准备为给定格式,然后将其附加到用户可以发送的电子邮件中。我正在使用 UIActivity 的子类,我正在做所有的工作-(void)activityViewController
:
- (UIViewController *)activityViewController
{
[self.alert show];
NSString *filename = [NSString stringWithFormat:@"%@.gpx", self.activity.title];
__block MFMailComposeViewController *mailComposeVC = [[MFMailComposeViewController alloc] init];
mailComposeVC.mailComposeDelegate = self;
[mailComposeVC setSubject:[NSString stringWithFormat:@"GPX export for %@ activity", self.activity.title]];
[mailComposeVC setMessageBody:@"Generated with Slopes" isHTML:NO];
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
CBCFileExporter *exporter = [[CBCFileExporter alloc] init];
NSData *exportContents = [exporter exportActivity:self.activity inFileFormat:CBCFileExportTypeGPX error:nil];
[mailComposeVC addAttachmentData:exportContents mimeType:@"application/gpx+xml" fileName:filename];
});
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
return mailComposeVC;
}
我遇到的具体问题是 UIAlertView 直到 dispatch_sync 完成才真正显示。我意识到 dispatch_sync 可能(?)在等待时阻塞了主线程,但问题是我需要等到附件生成后再从该方法调用返回(MFMailComposeViewController 文档说一旦视图是你就不能添加附件提出了)。
当主线程必须等待完成的非平凡任务必须运行时,如何获得警报视图以显示?