2013-08-20 8 views
3

다음 설정이 있습니다. 내 응용 프로그램이 UIDocument 인스턴스를 iCloud에 씁니다. 파일이 동기화되고 모든 것이 올바르게 작동합니다. 하지만 삭제하려고하면 다시 나타납니다. 파일을 삭제하기 전에 파일을 닫습니다.iCloud 파일이 삭제 된 후에 계속 나타납니다.

- (void)deleteDocumentWithName:(NSString*)name completion:(void (^)(BOOL success))completion 
{ 
[self stopQuery]; 
NSURL* toDelete = [self URLForFileWithName:name]; 

UIDocument* doc = [[UIDocument alloc] initWithFileURL:toDelete]; 

void (^deleteDocument)() = ^() { 
    // Wrap in file coordinator 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { 

     NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 
     [fileCoordinator coordinateWritingItemAtURL:toDelete 
              options:NSFileCoordinatorWritingForDeleting 
               error:nil 
             byAccessor:^(NSURL* writingURL) { 
              // Simple delete to start 
              NSFileManager* fileManager = [[NSFileManager alloc] init]; 
              NSError* error = nil; 
              [fileManager removeItemAtURL:writingURL error:&error]; 

              if (error) { 
               LogError(@"%s - error while deleting file: %@", __PRETTY_FUNCTION__, error); 
              } 

              dispatch_async(dispatch_get_main_queue(), ^{ 
               [_files removeObject:toDelete]; 

               if (completion) { 
                completion((error == nil)); 
               } 

               [self startQuery]; 
              }); 
             }]; 
    }); 
}; 

if (doc) { 
    if (doc.documentState == UIDocumentStateNormal) { 
     [doc closeWithCompletionHandler:^(BOOL success) { 
      if (success) { 
       deleteDocument(); 
      } else { 
       if (completion) { 
        completion(NO); 
       } 
      } 
     }]; 
    } else { 
     deleteDocument(); 
    } 
} 
} 

이 방법은 내가 여러 NSMetadataQueryDidFinishGatheringNotifications을 얻을 실행 후 다음은 파일을 삭제 코드입니다. 먼저 삭제 된 파일을 포함하지 않습니다. 그런 다음 다른 NSMetadataQueryDidFinishGatheringNotification이 발견되고 삭제 된 파일이 다시 포함됩니다.

이유를 찾을 수 없습니다. 어떤 도움을 주셔서 감사합니다.

+0

왜 문서를 삭제하기 전에 설정 했습니까? 그건 불필요한 것 같습니다. 추측 하건데 나는 디스크에서 파일을 지우고 있다고 말 하겠지만, UIDocument의 자동 저장 기능이 실행되고 다시 저장됩니다. 삭제를 수행 할 때 폐쇄 된 것이 확실합니까? 또한 쿼리가 설정되는 방법에 따라 다르지만 삭제를 수행하는 동안 쿼리를 중지하고 시작하는 것은 불필요한 것으로 보입니다. stopQuery에 대한 문서에서 '수신자가 처리되지 않은 결과 수집을 완료했습니다.' –

+0

응답 해 주셔서 감사합니다. 나는 문제가 무엇인지 알아 내지 못했지만 전체 스레딩과 백그라운드 스터드를 제거하고 파일을 직접 삭제함으로써 파일 관리자가 문제를 해결했다고 비난했습니다. 나를 위해 코드는 이제 약간 버그가 많고 더러운 것 같습니다. 그러나 어이, 그것은 작동한다 ;-) – ralphbert

+3

그것은 좋다! 나중에 부활하는 문서가 있으면 다른 동일한 문서의 복사본이있는 다른 인스턴스 변수가 없는지 확인하십시오. 아무리 자주 디스크를 지우더라도 UIDocument 인스턴스가 앱의 다른 곳에 열려있는 사본에 매달려 있으면 자동 저장이이를 부활시킬 수 있습니다. 당신은 * 인스턴스 *가 파일의 복사본도 가지고 있는지 확인해야합니다. 하지만 IMHO 스레딩과 배경 작업은 작동 할 때 정말 멋지지만 일하기 위해 엉덩이에 통증을 느낄 수 있습니다! 조심해야 할 많은 사람들이 있습니다. 그것은 그것의 가치가있다. –

답변

0

비슷한 문제가 발생했습니다. NSFileCoordinator 인스턴스화의 캡슐화를 GCD에서 제거하면 삭제 작업을 수행 할 수 있습니다 (실제로이 작업을 백그라운드 스레드에서 수행하는 것이 바람직 함을 문서에도 불구하고 실제로 수행 할 수 없으므로 이상하게 발생 함). 처음에는

void (^deleteDocument)() = ^() { 
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; 
     [fileCoordinator coordinateWritingItemAtURL:toDelete 
      options:NSFileCoordinatorWritingForDeleting 
      error:nil 
      byAccessor:^(NSURL* writingURL) { 
      // Simple delete to start 
      NSFileManager* fileManager = [[NSFileManager alloc] init]; 
      NSError* error = nil; 
      [fileManager removeItemAtURL:writingURL error:&error]; 

      if (error) { 
      LogError(@"%s - error while deleting file: %@", __PRETTY_FUNCTION__, error); 
      } 

      [_files removeObject:toDelete]; 

      if (completion) { 
      completion((error == nil)); 
      } 

      [self startQuery]; 
     }]; 
}; 
관련 문제