如何根据 Firebase 实时数据库中的另一个节点更新特定节点(Android)?

huangapple go评论62阅读模式
英文:

How to update a specific node based on another node in firebase realtime database android

问题

我有两个父节点,每个节点都有一些子节点。当我尝试从一个节点更新一个子节点时,另一个子节点也应该更新。例如,当我从一个节点更新时间段(子节点)从09:00 AM到10:00 AM时,它应该在另一个节点中反映出来。
就像在SQL数据库中的外键概念一样。如果我们更新一个主键值,那么在引用它的另一个表中也会更新。

如何根据 Firebase 实时数据库中的另一个节点更新特定节点(Android)?

英文:

I have two parent node with some child. when i try to update one child from one node then another child node also should update. for example when i update time_slot (child node) 09:00AM to 10:00AM from one node then it should reflect in another node also.
Like in sql database foreign key concepts. if we update one primary key value then it will update in another table in which it is referred

如何根据 Firebase 实时数据库中的另一个节点更新特定节点(Android)?

答案1

得分: 2

Firebase 是一个NoSQL数据库。因此,您不能期望在这里获得SQL类型的行为。
您可以在移动应用程序中直接更新两个节点,或者第二个选择是使用云函数来触发数据库事件。

要了解云函数,请查看此链接

英文:

Firebase is a noSQL database. So you cant expect SQL type behavior here.
What you can do here is update both nodes directly from your mobile application. Second option is use Cloud Functions to trigger a database event.

To understand cloud functions please go through this .

答案2

得分: 0

要同时更新,请尝试以下方法:

```java
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests").child("1578311274538");
DatabaseReference timeRef = FirebaseDatabase.getInstance().getReference().child("TimeSlot").child("01").child("01");
Map<String, Object> updates = new HashMap<String,Object>();
Map<String, Object> timeUpdates = new HashMap<String,Object>();
updates.put("time_slot", newdate);
timeUpdates.put("time_slot", newdate);

ref.updateChildren(updates);
timeRef.updateChildren(timeUpdates);

基本上,使用 updateChildren() 来更新 Firebase 中的数据。这是唯一的方法,您需要引用要更新的属性上面的键。


<details>
<summary>英文:</summary>

To update both, try the following:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests").child("1578311274538")
DatabaseReference timeRef = FirebaseDatabase.getInstance().getReference().child("TimeSlot").child("01").child("01");
Map<String, Object> updates = new HashMap<String,Object>();
Map<String, Object> timeUpdates = new HashMap<String,Object>();
updates.put("time_slot", newdate);
timeUpdates.put("time_slot", newdate);

ref.updateChildren(updates);
timeRef.updateChildren(timeUpdates);


Basically, use `updateChildren()` to update the data in firebase. This is the only way and you need to have reference to the keys above the attribute that you need to update.

</details>



# 答案3
**得分**: 0

你需要创建 `Request` 和 `TimeSlot` 的模型类。

```kotlin
var firebaseApp = FirebaseApp.initializeApp(chatActivity)
var firebaseDatabase = FirebaseDatabase.getInstance()
var Db = firebaseDatabase?.getReference().child("Request").addValueEventListener(object : ValueEventListener {
    override fun onCancelled(p0: DatabaseError) {

    }

    override fun onDataChange(p0: DataSnapshot) {
        // p0 包含 Request 节点的所有数据
        // 现在我们将获取其子项
        for (request in p0.children) {
            var requestModel = request.getValue(RequestModel::class.java) as RequestModel
            // requestModel 包含所有 157... 的数据
        }
    }
})

你可以为所有请求创建一个列表。
英文:

You need to create model class for Request and also for TimeSlot.

    var firebaseApp =FirebaseApp.initializeApp(chatActivity)
    var firebaseDatabase=  FirebaseDatabase.getInstance()
    var Db=firebaseDatabase?.getReference().child(&quot;Request&quot;).addValueEventListener(object :ValueEventListener{
        override fun onCancelled(p0: DatabaseError) {

        }

        override fun onDataChange(p0: DataSnapshot) {
            //p0 have all the data off Request node 
            //now we will fetch its childern
            for (request in p0.children)
            {
              var requestModel=timeMsg.getValue(RequestModel::class.java) as RequestModel
             //requestModel have the all the data of 157...

             }
        }
    })

you can make the list for all request.

huangapple
  • 本文由 发表于 2020年1月6日 20:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定