停止快照侦听器

时间:2018-04-14 09:49:34

标签: ios swift google-cloud-firestore

我有一个快照监听器,我希望在我解雇ViewController时停止它,我知道我需要使用listener.remove(),但我想在ViewWillDisappear()函数中调用它。

我想在ViewController中声明一个var:

var listener = ListenerRegistration?.self

然后在侦听器函数类型中:

listener = openInstanceRef?.collection("MyDishes").addSnapshotListener( ... )

但是我收到了这个错误:

Cannot assign value of type 'ListenerRegistration?' to type 'ListenerRegistration?.Type'

如果我尝试删除.self并输入ListenerRegistration?()而不是在侦听器声明中出现此错误:

`Cannot invoke initializer for type 'ListenerRegistration?' with no arguments`

swift无法向我提供任何可获得参数的初始订阅者

由于

2 个答案:

答案 0 :(得分:0)

Swift中的语法是:

listener.remove()

请参阅Firestore documentation on removing listeners

答案 1 :(得分:0)

您可以改为这样初始化

private var listener = ListenerRegistration!

,如果可选,只需删除括号

private var listener = ListenerRegistration? = nil