2012-08-10 87 views
1

我在用於管理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]; 
    }); 
}); 
+0

此代碼不工作我沒有時間爲您調試它看到答案 – 2012-08-10 14:50:16

+0

也許你應該使用NSFileCoordinatorWritingForMoving – Andy 2015-12-19 09:48:51

回答

0

setUbiquitous:假去除故事的結束iCloud的文件,並在目標位置放一個副本。

從ubiquity容器中刪除文件會從iCloud中刪除該文件,故事結束不會移動完成。

移動文件移動文件。

在iCloud之上構建了5個應用程序,我可以告訴您需要查看您的代碼。

從iCloud中刪除文件並放在本地機器上:setUbi ... FALSE目標。

從iCloud中

的NSFileManager removeFile刪除文件 - 無處不在的容器。

你的工作方式太難了。

+0

我已經嘗試過'setUbiquitous:false';在目標位置進行復制,有時文件不會從雲中刪除。我可以調用一個函數來強制iCloud同步嗎? – jblais 2012-08-10 15:30:46

+0

來自雲端還是來自本地無處不在的容器?差異很大 – 2012-08-10 15:44:23

+0

來自本地容器。由於本地容器應該是雲中鏡像的鏡像,我並沒有真正看到這裏的區別...... – jblais 2012-08-10 18:39:32

相關問題