我在用於管理iCloud上的文件的函數中使用此代碼。當我將它從雲中移動到本地計算機時,大約有三分之一的機會只是複製文件,而不是移動文件。但是,對moveItemAtURL
的呼叫返回YES,並且沒有錯誤。除了這個調用,雲中沒有任何操作(我不會同時讀取或寫入其他文件)。使用[moveItemAtURL:toURL:error:]將文件移動到iCloud:僅複製
我也試過setUbiquitous:itemAtURL:destinationURL:error:
函數,但結果是一樣的。
有沒有可能文件被鎖定,所以它只能被複制而不能移動?或者它是一個同步問題?
感謝
dispatch_async(operationQueue, ^(void) {
__block NSError *err1 = nil;
__block NSError *err2 = nil;
NSFileManager * fm = [ NSFileManager defaultManager];
__block bool success = true;
NSFileCoordinator *fc = [[[ NSFileCoordinator alloc ] initWithFilePresenter: nil ] autorelease ];
[ fc coordinateWritingItemAtURL : i_srcDocumentURL options:NSFileCoordinatorWritingForReplacing
writingItemAtURL: i_dstDocumentURL options:NSFileCoordinatorWritingForReplacing
error : &err1 byAccessor:
^(NSURL *newURL1, NSURL *newURL2) {
[ fm removeItemAtURL: newURL2 error:nil];//Remove destination file in case there is one
if( [ fm moveItemAtURL:newURL1 toURL:newURL2 error:&err2 ] == FALSE)
{
success = false;
}
} ];
if(err1)
{
success = false;
[self logError:err1];
}
if(err2)
{
success = false;
[self logError:err2];
}
dispatch_async(mainQueue, ^(void) {
if (success) {
i_answerObj->MoveFileOk([i_fileName UTF8String]);
}
else
{
i_answerObj->MoveFileError([i_fileName UTF8String]);
}
[self destroyMyself];
});
});
此代碼不工作我沒有時間爲您調試它看到答案 – 2012-08-10 14:50:16
也許你應該使用NSFileCoordinatorWritingForMoving – Andy 2015-12-19 09:48:51