urban-health-labs / CombineFirebase

Combine wrapper on Google's iOS Firebase library.
MIT License
225 stars 44 forks source link

Does this Library remove listeners automatically? #22

Closed otymartin closed 2 years ago

otymartin commented 2 years ago

@kshivang

According to the Firebase docs

When you are no longer interested in listening to query realtime updates, you must detach your listener so that event callbacks stop. This allows the client to stop using bandwidth to receive updates. 

So I'm curious how is it that CombineFirebase handles this as I can't find any code referencing the removal or detachment of these listeners. I'm concerned with bandwidth usage which can get expensive.

Is CombineFirebase doing this automatically somehow or are we responsible for it?

kshivang commented 2 years ago

CombineFirebase is exactly for this. Purpose of combineFirebase is to give firebase listener api lifecycle of a publisher, which should be the default lifecycle for any api in new Combine Framework paradigm. Default lifecycle of a publisher is, data is being listened after at-least one subscriber being subscribed to publisher, this is done via sink and assign api. Both sink and assign api in turn return Cancelable, which you can use to stop listener by using [cancel()](https://developer.apple.com/documentation/combine/cancellable/cancel()) method of cancellable. Basically all the api of combineFirebase remove listeners on cancel method invocation. You can dig in source here, to understand its implementation, this is exactly what firebase is recommending in there docs. Hope that answer your concern. 🙌

otymartin commented 2 years ago

@kshivang Ahh very informative piece. I always store in my cancelbag because compiler demands it, didn't care to understand it until now.

I saw cancel() method in your code but couldn't figure out why it wasnt called but I see it in the Cancellable protocol.

Thanks very educational explanation.