I think most people who come into contact with Firebase want to use "** Realtime Database **". (Rogue)
However, the only articles that come out after researching are ~~ off-target ~~. I think that it will be necessary to verify to some extent in order to use it properly. Of course, verification is very important, and I understand that it is the real thrill.
However, there are people (including myself, of course) who are likely to rampage the inner beast, saying "** I want to use it for the time being! **".
For those people, I'll put together the code so that you can copy and paste it as much as possible.
** "I don't understand this, too" "Copy and paste is s (omitted" for learning ... etc. It will be very embarrassing for people, so please browser back. **
I don't think I will use it first For example, if you want to save only the character string "** Hello World !**", write it like this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
myRef.setValue("Hello World !");
For example, if you want to save the character string "** apple " in the saved data " Items **", write it like this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Items");
myRef.setValue("apple");
This can be included as much as you want using . Child , For example, if you want to save the character string "" sweet "" in the character string "** apple " in the saved data " Items **", write it like this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Items").child("apple");
myRef.setValue("sweet");
In other words, you can also do this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Items").child("1").child("2").child("3").child("4").child("5").child("6").child("7").child("8").child("9").child("10");
myRef.setValue("good job");
Because Firebase is excellent, if there is no parent data, it will create it as well.
You can save the data held by your own model as it is. It's very easy to write ** (or rather, as it is), just put it in the same way.
Item item=new Item();
item.setName("apple");
item.setTaste("sweet");
item.setPrice(500);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Items").child("apple");
myRef.setValue(item);
This will save 3 data in "** apple " of " Items **".
"** The real thrill of Realtime Database is that it is synchronized in real time! **" ~~ Everyone wants to do this, so they're doing Firebase. ~~ rest assured. Firebase is also made on the premise of this, so conversely, you can not get data unless you use this.
For example, if you want to get the data when the data in the parent data "** Items **" is changed, write like this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference userRef = database.getReference("Items");
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get an instance
for (DataSnapshot data : dataSnapshot.getChildren()) {
Log.d("",data.getValue().toString);
}
}
@Override
public void onCancelled(DatabaseError error) {
//Failed to get data
}
});
If "** apple ", " pen " and " gorilla " are saved in " Items **", then ** size of ** dataSnapshot.getChildren () ** ** becomes 3 apple pen gorilla Should be logged.
For example, if the above custom model was saved, write it like this.
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference userRef = database.getReference("Items");
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get an instance
for (DataSnapshot data : dataSnapshot.getChildren()) {
Item item=data.getValue(Item.class);
}
}
@Override
public void onCancelled(DatabaseError error) {
//Failed to get data
}
});
Recommended Posts