[英]user.uid.501.BRNotificationServerAvailabilityChanges error in Objective-c
我正在執行此代碼以從 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.");
}
我收到此錯誤
[default] [ERROR] notify_get_state(241) failed with 'invalid_token' (2) for 'user.uid.501.BRNotificationServerAvailabilityChanges'
由於此錯誤,它不會觸發
NSMetadataQueryDidFinishGatheringNotification
通知中心。
任何人以前都面臨過這樣的問題。
我遇到了同樣的問題並弄清楚出了什么問題。 該錯誤基本上意味着NSMetadataQuery
變量在 function 完成時在它實際完成收集信息之前被釋放。 您可以通過將元數據查詢聲明為 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()
}
}
另外不要忘記將管理器實例保留為UIViewController
等的屬性。
final class SomeViewController: UIViewController {
private let manager = UbiquitousFileManager()
}
我遇到了同樣的問題。
我最終發現的是,如果我在獨立的 class 中運行代碼,即使從 NSObject 派生,我也會收到該錯誤。
一旦我將代碼移動到 UIViewController class 中,它就可以工作。
但是,仍然不知道原因是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.