【问题标题】:Swift Firebase Database Array of AutoIDAutoID 的 Swift Firebase 数据库数组
【发布时间】:2017-03-21 04:36:06
【问题描述】:

  1. 我想将来自所有 ChildDBYAutoID 的每个图形项接收到一个双精度数组中。

  2. 另外,有没有更好的方法通过计数来做到这一点,所以没有自动 ID?比如例子:

    0 724 1 744 2 745 3 800 . . .

我的主要目标是上传许多图表值,而不仅仅是更新一个。然后将图形值检索到一个双精度数组中。

func uploadToFirebase(){

   //Firebase Initialization
    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()

    ref.child("general_room_index").childByAutoId().setValue(["graph_value": totalCountY])

}

databaseRef.child("general_room_index").observeSingleEventOfType(.Value, withBlock: { (snapshot) in

  snapshot.value!["medals"] as! [Double]

    })

【问题讨论】:

  • 用你的数组试试enumerate
  • 所以基本上你想在你的firebase general_room_index 节点中存储一个[Int:Int] 字典并以相同的方式检索??

标签: ios swift firebase swift3 firebase-realtime-database


【解决方案1】:

据我了解您的问题,您必须将 JSON 结构更改为:-

genera_Room_Index_Count : 3,

genera_Room_Index : {
       1 : 123,
       2 : 321, 
       3 : 565
          }

将您的genera_Room_Index_Count 初始化为 0;相同的安全规则将适用于genera_Room_Index_Count 节点;然后开始附加值

 func uploadToFirebase(your_Value : Int){   // your_Value is your graph value as parameter

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

        let count = Counts.value as! Int + 1

        print(count)

        FIRDatabase.database().reference().child("genera_Room_Index").child(String(describing: count)).setValue(your_Value, withCompletionBlock: { (Err, Ref) in

            print(Err ?? "No error")
            print(Ref)

            if Err == nil{

                FIRDatabase.database().reference().child("genera_Room_Index_Count").runTransactionBlock({ (currentData) -> FIRTransactionResult in

                    var value = currentData.value as? Int

                    if value == nil {
                        value = 0
                    }

                    currentData.value = value! + 1

                    return FIRTransactionResult.success(withValue: currentData)

                })
            }
        })
    })
}

安全规则

"genera_Room_Index" :{

     ".read" : "true",    // Or whatever your authentication flowchart might be
    ".write" : "true",    // Or whatever your authentication flowchart might be


  },

  "genera_Room_Index_Count" :{

     ".read" : "true",     // Or whatever your authentication flowchart might be
    ".write" : "true",     // Or whatever your authentication flowchart might be 


  },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2019-04-28
    • 2019-07-28
    相关资源
    最近更新 更多