8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(swift) firebaseでobserverをremoveするのにハマった

Last updated at Posted at 2016-10-31

swiftで以下のようにobserveEventを設定した時になんらかのタイミングでobserverをremoveすることがあるのだが、、、

.observeEventType(
    .Value,
    withBlock: {
        (snapShots) in
    }
)

この時にとりあえず

var ref = FIRDatabase.database().reference
ref().removeAllObservers()

とやっていればいいと思ったら違ったのでメモ

例えば以下のようにobserveEventを設定した時

ref().child("rooms").child("room_id").child(roomId!).child("users").observeEventType(.Value, withBlock: {(snapShots) in
})

こういう時は丁寧に

ref().child("rooms").child("room_id").child(roomId!).child("users").removeAllObservers()

としなければならない

ちなみにreferenceにはこう書いている

Removes all observers at the current reference, but does not remove any observers at child references. removeAllObservers must be called again for each child reference where a listener was established to remove the observers.

8
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?