【发布时间】:2013-10-23 09:38:19
【问题描述】:
我无法理解 UIDocument 异步打开的方式。我有一系列用户创建的 UIDocument。当用户按下按钮时,我需要打开一些文档,从中提取一些字符串,然后将这些字符串连接到一个新文件中。
我有一个数组来跟踪需要打开哪些文档。我的想法是我应该遍历数组,打开每个文档,并将其内容附加到NSMutableString。问题是openWithCompletionHandler 异步执行其工作,因此循环前进并结束,在文档被打开(并再次关闭)之前返回一个空字符串。
这里有一点代码:
__block NSMutableString *combinedString;
for (entryClass *entry in documentList)
{
MyDocumentClass *document = [[MyDocumentClass alloc] initWithFileURL:[entry fileURL]];
if ([document documentState] & UIDocumentStateClosed) {
[document openWithCompletionHandler:^(BOOL success) {
if (success) {
[combinedString appendString:[document documentBody]];
}
[document closeWithCompletionHandler:nil];
}];
}
}
NSLog(@"Combined String: %@", combinedString);
很自然地,combinedString 会返回为空,因为在后台打开文档时循环结束。我可以将文档处理移动到它自己的返回字符串的方法,但我认为这仍然会在文档被读取之前返回。我大概必须设置一个进度指示器并让用户等待——这可能没问题。
【问题讨论】:
标签: ios objective-c uidocument