H.Epstein
H.Epstein

Reputation: 731

Stop snapshot listener

I have a snapshot listener and I want to stop it when I dismissing my ViewController, I know I need to use listener.remove() but I want to call it in the ViewWillDisappear() function.

I want to declare a var in my ViewController:

var listener = ListenerRegistration?.self

and then in the listener func type :

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

but I get this error :

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

if I try to remove the .self and type ListenerRegistration?() instead I get this error in the declaration of listener :

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

but swift won't offer me any initialisers that do get arguments

Thanks

Upvotes: 0

Views: 1905

Answers (2)

Hugo Perez
Hugo Perez

Reputation: 481

You can initialise like this instead

private var listener = ListenerRegistration!

and if its optional just remove the parenthesis

private var listener = ListenerRegistration? = nil

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The syntax in Swift is:

listener.remove()

See the Firestore documentation on removing listeners.

Upvotes: 1

Related Questions