Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleted UIDocument often reappears from cloud

Tags:

icloud

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?

like image 820
Jonny Avatar asked Jun 25 '12 08:06

Jonny


3 Answers

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

like image 101
Jonny Avatar answered Nov 18 '22 15:11

Jonny


One possible source of problems in this area is that the documentState of your UIDocument passes through these states sequentially on delete...

8 (UIDocumentStateEditingDisabled)

then 0 (UIdocumentStateNormal) probably to update the filename,

then 4 (UIDocumentStateSavingError) to delete.

Therefore the deletion may be fighting against your code that fires when you receive a UIDocumentStateChangedNotification saying that the state is normal.

I hope this helps someone.

UPDATE

Instead of trying to drive everything from DocumentState, as I tried unsuccessfully for a month to do (following Wenderlich examples)...

The key for me was adding accommodatePresentedItemDeletionWithCompletionHandler to the subclassed UIDocument, containing a closeWithCompletionhandler: call that closes the open file on all devices as deletion is detected. It then posts a notification to my manager to remove instances of this item from the source data array.

This seems much more robust and predictable. I do still see several DocumentState 8 and State 9s arriving to each device following a deletion, where I would expect to only see one, but that may be a different issue.

like image 40
Peter Johnson Avatar answered Nov 18 '22 16:11

Peter Johnson


In my case, "accommodatePresentedItemDeletionWithCompletionHandler:" works to stop once-deleted-files reappearing, by force closing when the UIDocument is unconsciously opened. You can see the usage of it here: iCloud - renaming open documents on another device sometimes fails

like image 1
user1263865 Avatar answered Nov 18 '22 15:11

user1263865