ios - Swift Firebase 移除观察者不移除

标签 ios swift firebase firebase-realtime-database observers

我正在使用 Firebase 监听器来更新远程摄像头的状态值。一旦我在相机生命周期中循环,我想移除监听器,这样我的相机就不会重新开始并继续拍摄视频。

这是我到目前为止根据 SO 建议所做的事情:

1) 添加了 FIRDatabaseHandle 并调用了 removeObserver(withHandle: handle)/运气不好

2) 从根引用简单调用 removeAllObservers() 到您在下面看到的内容。

struct CameraActions {

let db = DataService.ds.db // this comes from a singleton used to for other Firebase calls
let uid = DataService.ds.curUser?.uid

var cameraRef:FIRDatabaseReference!

mutating func addCameraListener(cameraNum num:String, complete:@escaping(CameraStatus)->Void){
    cameraRef = db.child("camera").child(num).child("status")
    cameraRef.observe(.value, with: {
        snap in
        if let status = snap.value as? Int {

            switch status {
            case 0: complete(.ready)
            case 2: complete(.isRecording)
            case 4: complete(.hasStopped)
            case 5: complete(.problem)
            default:    print("App is waiting on camera")
            }
        }
    })
}

func cameraHasFinishedRecording(cameraNum num: String) {
    cameraRef.removeAllObservers() // latest attempt here
    db.child("camera").child(num).child("status").setValue(0) // this still triggers database call

}

在此先感谢您的帮助。

最佳答案

Firebase 的工作原理与宣传的完全一样。观察者的移除是有效的,但是,另一个应该是单一观察者的观察者正在开火。感谢您的投入,很抱歉浪费您的时间。

干杯!

关于ios - Swift Firebase 移除观察者不移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42686466/

相关文章:

ios - 无法在 neSTLed CollectionView 中获取 TableViewCell 的 indexPath

ios - 在 iOS 中移动 Google map 信息窗口

swift - 由于 'value' 保护级别切换语句,Alamofire 'internal' 无法访问

swift - 如何使用 Firebase 和 Swift 快速获取关注者和关注者数量

ios - ionic 3 输入项在 iOS 上不显示插入符号

ios - 使用 Asset Catalog 特权从 URL 下载图像

ios - 自定义 UIView 子类中的 UIButton 不触发操作事件

firebase - 如何在 Firebase Cloud Firestore 中保存 GeoPoint?

Firebase 存储 : Using Download Url Instead of Storage Ref

objective-c - 是否可以使用 sharedHTTPCookieStorage 为 UIWebView 手动设置 cookie?