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

守給你的承諾、 提交于 2020-01-26 04:03:19

问题


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


回答1:


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:


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.




回答3:


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("Request").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.



来源:https://stackoverflow.com/questions/59611877/how-to-update-a-specific-node-based-on-another-node-in-firebase-realtime-databas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!