使用[UIDocument saveToURL:forSaveOperation:completionHandler:]
调用时UIDocumentSaveForCreating
的行为是什么?
然后无需关闭文档。
MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
if (!saveSuccess) {
ALog(@"Failed to create file at %@", fileURL);
failureBlock();
return;
}
successBlock(fileURL);
}];
然后我必须自己关闭文档,如下:
MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
if (!saveSuccess) {
ALog(@"Failed to create file at %@", fileURL);
failureBlock();
return;
}
[document closeWithCompletionHandler:^(BOOL closeSuccess) {
if(!closeSuccess) {
ALog(@"Error during close after creating %@", fileURL);
failureBlock();
return;
}
successBlock(fileURL);
}];
}];
答案 0 :(得分:0)
Aha意识到UIDocument有documentState
属性。
在第1步中,documentState返回UIDocumentStateNormal
,因此它是打开的。
在第二步中,documentState返回UIDocumentStateClosed
,因此它已关闭。
MYDocument *document = [[MYDocument alloc] initWithFileURL:fileURL];
[document saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL saveSuccess) {
if (!saveSuccess) {
NSLog(@"Failed to create file at %@", fileURL);
failureBlock();
return;
}
NSLog(@"step1: documentState: %d", document.documentState);
[document closeWithCompletionHandler:^(BOOL closeSuccess) {
if(!closeSuccess) {
NSLog(@"Error during close after creating %@", fileURL);
failureBlock();
return;
}
NSLog(@"step2: documentState: %d", document.documentState);
successBlock(fileURL);
}];
}];
所以 NO 就是我自己的问题
UIDocumentSaveForCreating在创建后是否关闭文档?