【问题标题】:Swift Firebase removal of observers not removingSwift Firebase 移除未移除的观察者
【发布时间】:2017-07-29 21:56:24
【问题描述】:

我正在使用 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

}

提前感谢您的帮助。

【问题讨论】:

  • 你能看看你调用了多少次addCameraListenerfunc
  • 您是否也要检查 db 期间 记录中的所有值更改?还是只在录制开始时进行一次?
  • 我的 VC 在 ViewDidLoad 中调用监听器(所谓的一次设置)。根据侦听器返回值的状态加载各种视图。是的,我确实会在记录过程中检查值的变化,这些变化在上面未列出的函数中以便浓缩。

标签: ios swift firebase firebase-realtime-database observers


【解决方案1】:

Firebase 的工作方式与宣传的完全一样。观察者的移除正在起作用,但是,另一个应该是单个观察者的观察者正在开火。感谢您的意见,很抱歉浪费您的时间。

干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2012-01-16
    相关资源
    最近更新 更多