iosios6icloudnsmetadataqueryicloud-api

Problems handling NSMetadataQueryDidUpdateNotification non-UIDocument based iCloud files


I've already got headaches when trying to solve this: in my current app I'm using iCloud to store my documents - but I can't use UIDocument based storage for the data. So my approach was to follow WWDC 2012 article 237 (advanced iCloud document storage) and treating my documents like a shoe box app.

The real problem occurs when I build up a NSMetadataQuery to monitor changes in the iCloud document folder to watch out for file changes. I've setup my query, start it with an observer for NSMetadataQueryDidFinishGatheringNotification and when it fires I enable updates for the query and setup an observer for NSMetadataQueryDidUpdateNotification. The latter only works once for one file change and I didn't find out how to get it fired repeatedly for subsequent changes of the same or other files. Here are some code parts:

- (void)startMonitoringDocumentsFolder: (id)sender
{
    if (self.queryDocs != nil)
        return;

    NSLog(@"startMonitoringDocumentsFolder:");

    self.queryDocs = [[NSMetadataQuery alloc] init];
    [queryDocs setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

    NSPredicate*    pred = [NSPredicate predicateWithFormat:@"%K like '*.nwa' or %K like '*.NWA'", NSMetadataItemFSNameKey, NSMetadataItemFSNameKey];

    [self.queryDocs setPredicate:pred];

    NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
    NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

    // I'm using the blocks version because I'm planning to setup observers for the same tracking name...
    self.observeGatherDocs = [center addObserverForName: NSMetadataQueryDidFinishGatheringNotification
                                                 object: self.queryDocs
                                                  queue: mainQueue
                                             usingBlock: ^(NSNotification* note)
    {
        [self queryDidFinishGathering:note];
    }];
    [self.queryDocs startQuery];
}

- (void)queryDidFinishGathering:(NSNotification *)notification
{
    NSLog(@"queryDidFinishGathering:");
    NSMetadataQuery*    query = [notification object];

    if (query == queryDocs)
    {
        NSLog(@"--> for queryDocs");
        NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
        NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

        [center removeObserver: observeGatherDocs];
        self.observeGatherDocs = nil;
        self.observeUpdateDocs = [center addObserverForName: NSMetadataQueryDidUpdateNotification
                                                     object: query
                                                      queue: mainQueue
                                                 usingBlock: ^(NSNotification* note)
        {
            [self queryDidUpdateDocumentsFolder:note];
        }];

        [query enableUpdates];
    }
}

- (void)queryDidUpdateDocumentsFolder:(NSNotification *)notification
{
    NSLog(@"queryDidUpdateDocumentsFolder:");
    NSMetadataQuery*    query = [notification object];

    [query disableUpdates];

    for (NSMetadataItem* item in [query results])
    {
        // find and handle file changes here w/out any command calls to the query itself
        ...
    }

    if (self.needDownload)
        [self triggerDownloadThread];

    // ---- BEGIN code part added to solve the problem ---- //
    // re-create the observer
    NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
    NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

    [center removeObserver: observeUpdateDocs];
    self.observeUpdateDocs = [center addObserverForName: NSMetadataQueryDidUpdateNotification
                                                 object: query
                                                  queue: mainQueue
                                             usingBlock: ^(NSNotification* note)
    {
        [self queryDidUpdateDocumentsFolder:note];
    }];
    // ---- END code part added to solve the problem ---- //

    [query enableUpdates];
    // that's all with the query, am I missing something?
}

Any advice here is really very appreciated.

What it really does:

  1. When I modify a file within the iCloud document container directly (by overwriting) - nothing, the query doesn't fire.

  2. When I modify a file by the sequence setUbiquitous:NO, write to the local file, setUbiquitous:YES the query fires once, I detect the change and when I change the same file again there is no recall of the query handlers.

So despite I've found in the Apple docs that the way to write to iCloud files like 1. above applies to NEW files it seems that it is also necessary modifying existing files.

But, anyway, how can I get the query mechanism running repeatedly?

Cheers...


Solution

  • After investigating with some more effort I've found a remarkable solution. So if someone else is stumbling in I'm just sharing my experience:

    1. The observer instance variables should be kept as retaining references (or strong when using ARC)
    2. Before updates are enabled the observer must be re-created.

    That seems to be all. For now it seems to work.