0

I am executing this code to get files/document from iCloud

NSMetadataQuery *metadataQuery = [[NSMetadataQuery alloc] init];
[metadataQuery setSearchScopes:
     [NSArray arrayWithObject:
      NSMetadataQueryUbiquitousDocumentsScope]];

[metadataQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE 'myfile.zip'", NSMetadataItemFSNameKey]];

[[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(metadataQueryDidFinishGathering:)
     name:NSMetadataQueryDidFinishGatheringNotification
     object:metadataQuery];

 if (![metadataQuery startQuery]) {
        NSLog(@"Start query not work for some reason.");
 }

I am getting this error

 [default] [ERROR] notify_get_state(241) failed with 'invalid_token' (2) for 'user.uid.501.BRNotificationServerAvailabilityChanges'

Due to this error, it doesn't fire

NSMetadataQueryDidFinishGatheringNotification

NotificationCenter.

Anyone face any such issue before.

Chirag Lukhi
  • 1,528
  • 17
  • 41
  • Are you separating this query search in another stand alone class and calling it from where the search is supposed to happen in the ViewController? – Jake A. Jul 26 '20 at 05:18

2 Answers2

2

I ran into the same issue and figured out what went wrong. The error basically means that the NSMetadataQuery variable was deallocated on function completion before it actually finishes gathering information. You can solve by declaring the metadata query as a property for a class.

class UbiquitousFileManager {
    private var metadata: NSMetadataQuery?
    
    func retrieve() {
        self.metadata = NSMetadataQuery()
        self.metadata!.predicate = NSPredicate(format: "%K like %@", NSMetadataItemFSNameKey, "somefile.txt")
        self.metadata!.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]

        NotificationCenter.default.addObserver(
            forName: .NSMetadataQueryDidFinishGathering,
            object: self.metadata!,
            queue: nil,
            using: { notification in
                // Do something with the result...
         })

        self.metadata!.start()
    }
}

Also do not forget to keep the manager instance as a property for the UIViewController etc.

final class SomeViewController: UIViewController {
    private let manager = UbiquitousFileManager()
}
Jake A.
  • 540
  • 5
  • 12
0

I run into the same problem.

What I eventually figured out is, that if I run the code in a stand alone class, even if derived from NSObject, I will get that error.

As soon as I move the code into a UIViewController class, it works.

No idea still what the reason is, though.

Michael Konz
  • 503
  • 1
  • 3
  • 20
  • Hey, could you please provide some more details about what happened, and how you fixed it? Which code exactly needs to run within a UIViewController? I just encountered this from a class (not derived from NSObject), and moved the Notification Handling code to a UIViewController, and am still getting this. Would appreciate your help. – Eytan Schulman Mar 30 '21 at 00:57
  • I am sorry, but I do not have that code any longer because some design changes made it obsolete. But if I remember correctly I put the complete query inside a UIViewController instance, not only the Notification handling. – Michael Konz Mar 31 '21 at 12:30