Question

I've found out, that I can detect the deletion of a UIDocument on the iCloud through following method:

- (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *))completionHandler

This method gets correctly called, but I don't know what to do in the method. At the moment I close the document, if it's still open, but it looks like the document gets saved on the old path while closing, so the document reappears.

I have already searched intensely, but I haven't found anything neither in the Apple doc nor in any forum.

Has somebody made similar experience or has somebody handled the deletion correctly?

Was it helpful?

Solution

I've found out, that I close the document 2 times and before closing it the 2nd time, I save it. I have remove the saveToURL method and now it works as expected.

For all who want to detect a deletion: Overwrite this method in your subclass of UIDocument with following code:

    - (void)accommodatePresentedItemDeletionWithCompletionHandler:(void (^)(NSError *errorOrNil))completionHandler
{
    sceneLampDocument* presentedDocument = self;
    [presentedDocument closeWithCompletionHandler: ^(BOOL success) {
        NSError* error = nil;
        if (!success)
        {
            NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"Could not close document that is being deleted on another device",
                                      NSLocalizedDescriptionKey, nil];
            error = [NSError errorWithDomain: @"some_suitable_domain"
                                        code: 101
                                    userInfo: userInfo];
        }

        completionHandler(error);  // run the passed in completion handler (required)

        dispatch_async(dispatch_get_main_queue(), ^
                       {
                           //[super accommodatePresentedItemDeletionWithCompletionHandler:completionHandler];

                           NSDictionary *userInfo = [NSDictionary dictionaryWithObject:self forKey:@"document"];
                           [[NSNotificationCenter defaultCenter] postNotificationName: @"documentDeletedOnAnotherDevice"
                                                                               object: self
                                                                             userInfo: userInfo];
                       });
        }];
}

I hope this will help someone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top