Search code examples
icloud

Deleted UIDocument often reappears from cloud


I'm working with documents in the cloud... * Add a document to the cloud * Delete that document from the cloud * Somehow that document reappears a few seconds later.

Here's the details:

I create an instance of UIDocument like this

    NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    NSURL *ubiquitousPackage = [[ubiq URLByAppendingPathComponent:@"Documents"] URLByAppendingPathComponent:@"somenewfilename"];

    MyDoc* docTemp = [[MyDoc alloc] initWithFileURL:ubiquitousPackage];
    docTemp.mapContent = [NSString stringWithString:self.currentdocument.mapContent];

    [docTemp saveToURL:[docTemp fileURL] 
      forSaveOperation:UIDocumentSaveForCreating 
     completionHandler:^(BOOL success) {            
         if (success) {
             DLog(@"New document %@ saved/added: %@", newFilename, docTemp);
         }
         else {
             DLog(@"Failed saving new document: %@", newFilename);
         }
         [docTemp release];
     }];

Then delete it later like this:

    NSURL* fileURL = self.currentdocument.fileURL;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)       {
    NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
    NSError* error = nil;
    [fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingForDeleting error:&error byAccessor:^(NSURL* writingURL) {
        if (error != nil) {
            DLog(@"Error with %@! %@", fileURL, error);
            return;
        }
        DLog(@"Got writingURL: %@", writingURL);
        NSFileManager* fileManager = [[NSFileManager alloc] init];
        if ([fileManager removeItemAtURL:writingURL error:nil]) {
            DLog(@"Deleted %@!", writingURL);
        }
        else {
            DLog(@"ERROR Failed deleting %@!", self.currentdocument);
        }
        [fileManager release];
        [fileCoordinator release];
    }];
});

Now despite doing the above, it looks like the file is deleted for a (short) while, but on several occasions the deleted document reappears in the cloud one or a few seconds later. (I can check this through iCloud settings on another device, or through a query that sends notifications on updates). Only on SOME occasions the file remains deleted. What's going on?

I should note that even deleting the file from Settings->iCloud will cause the above pattern. The file magically reappears a few seconds later. Timing issues? Sync issues?


Solution

  • Don't delete documents while they are open. :-P