LoginSignup
10
4

More than 5 years have passed since last update.

Firebase Realtime Database で特定の値を増加させる

Last updated at Posted at 2016-11-15

概要

Firebase/RealtimeDatabaseでインクリメンタルな値を扱う簡単な仕組みがない。
そのためのコードを記載する。
何らかのイベント回数などを初期化して累積増加させたい時に使用できる。

increment

FIRDatabase.database().reference().child("test1").observeSingleEvent(of: .value, with: { (snapshot) in

            var count:Int = 1
            if !(snapshot.value is NSNull){
                count = (snapshot.value as! Int) + 1
            }
            FIRDatabase.database().reference()
                                    .child("test1")
                                    .setValue(count)

        }) { (error) in
            print(error.localizedDescription)
        }

参考

10
4
0

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
10
4