Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:57:54+00:00 2026-06-16T13:57:54+00:00

I’ve already got headaches when trying to solve this: in my current app I’m

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:57:56+00:00Added an answer on June 16, 2026 at 1:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.