Chirag Lukhi
Chirag Lukhi

Reputation: 1546

user.uid.501.BRNotificationServerAvailabilityChanges error in Objective-c

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.

Upvotes: 0

Views: 242

Answers (2)

Jake A.
Jake A.

Reputation: 550

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()
}

Upvotes: 2

Michael Konz
Michael Konz

Reputation: 543

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.

Upvotes: 0

Related Questions