Add an item to a list in Firebase Database

前端 未结 4 2032
礼貌的吻别
礼貌的吻别 2021-01-01 16:14

I have the following Firebase database structure. uIds is a type of List. I am trying to add another uId under uIds with

4条回答
  •  无人及你
    2021-01-01 17:10

    Not sure what you mean when you say setValue, etc require you to retrieve existing data. The basic flow for inserting new record is as follows:

    private DatabaseReference mDatabase;
    // get reference to your Firebase Database.
    mDatabase = FirebaseDatabase.getInstance().getReference();
    //and here you add a new child to your 'requests' collection
    //I am assuming you have a Request model like this..
    Request newRequest = new Request(some-params);
    mDatabase.child("requests").child(someRequestId).setValue(newRequest);
    

    You can take a look at basic usage guide for Saving Data on Android Firebase.

    Update: Following your comment - I think what you are looking to do can be achieved like this: You use the push() method which generates a unique ID every time a new child is added to the specified Firebase reference:

    Firebase newRequestRef = mDatabase.child("request").push();
    newRequestRef.setValue(newRequest);
    

    This should do it.

    I hope this helps.

提交回复
热议问题