swift - NSNotification NSMetadataQueryDidUpdate 获取不间断的通知消息

标签 swift ios11 icloud-drive

我正在我的应用程序中实现保存到 iCloud Drive。现在我正在模拟器上测试该应用程序。我可以将文件保存到 iCloud Drive,它会按预期显示在驱动器上。在我的代码中,我实现了NotificationCenter.default.addObserver。当应用程序处于事件状态时,将调用 startQuery()。这是代码示例:

@objc func startQuery()
{
    stopQuery()
    query = documentQuery()
    print( "adding notifications" )
    NotificationCenter.default.addObserver(self, selector: #selector(finishGatheringNotification(notifciations:)), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object:nil )
    NotificationCenter.default.addObserver(self, selector: #selector(didUpdateNotification(notifciations:)), name: NSNotification.Name.NSMetadataQueryDidUpdate, object:nil )
    query?.start()
}

@objc func stopQuery()
{
    guard let query = self.query else{
        return
    }
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object:nil )
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidUpdate, object:nil )
    query.stop()
    self.query = nil

}

     @objc func finishGatheringNotification( notifciations:NSNotification ){
        print( "called finished gathering notification" )
        processICloudFiles(notifciations: notifciations )
    }

    @objc func didUpdateNotification( notifciations:NSNotification ){
        // This seems to be getting called non-stop
        print( "called updated notification" )
        processICloudFiles(notifciations: notifciations )
    }

    @objc func processICloudFiles( notifciations:NSNotification )
    {
        guard let query = query else{
            return
        }
        query.disableUpdates()
        var metaDataItems:[NSMetadataItem] = []

        if let queryResults = query.results as? [NSMetadataItem]{
            print( "query results:\(queryResults.count)")
            for result in queryResults{
                if let fileURL = result.value(forAttribute: NSMetadataItemURLKey ) as? NSURL{
                     print( "file found in iCloud:\(String(describing: fileURL.lastPathComponent))")
                    metaDataItems.append( result )
                }
            }
            if metaDataItems.count > 0{
                updateICloudFiles(metaDataItems: metaDataItems)
            }
        }
        query.enableUpdates()
    }

但是,NSMetadataQueryDidUpdate 的选择器被不停地调用。我认为它只在 iCloud 磁盘内容发生变化时才会被调用。
任何建议。
谢谢
礼萨

最佳答案

尝试取消初始化:

deinit {
    NotificationCenter.default.removeObserver(self)
}

关于swift - NSNotification NSMetadataQueryDidUpdate 获取不间断的通知消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47640039/

相关文章:

即使设置为 "never",也可以访问 iOS11 照片库

ios - Admob Banner 广告在 SpriteKit 切换场景时变为 Nil

ios - 此 bundle 无效。键 CFBundleShortVersionString 的值必须包含..Xcode Sticker app

swift - UILabel 的字体在 View 重新出现之前不会调整

swift - UISearchController 在 iOS 11 Swift 4 上隐藏状态栏

ios - iCloud Drive 和 CloudKit 有什么区别?

swift - Vapor 3 Swift 4 如何制作计时器

objective-c - iOS11 objective-c issue 'unrecognized selector sent to instance' 调用自定义类存在的方法

ios - 将文件保存在 iCloud Drive 文档中(用户访问)

ios - 如何使用 swift 以编程方式将 pdf 文档保存到 iCloud Drive?