Firebase Database Tutorial

DownloadFull Demo For all firebaseItem.

step 1:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();

//this give the root path like :> https://.firebaseio.com/

step 2:
We add on child element in root path :
 DatabaseReference mChild = ref.child("users");

//Here ref is the object of root element path.

step 3:
Simple Data save
mChild.setValue("Data Value Set Here");

Step 4:
simple Data Value Get here
//if its a String than

 String value= mChild.getValue(String.class);

======= NOW Some AdvanceLevel ====

Add Custom Model Class object :>
Model{
String name,time;

Model()
{
// Don't Forget Non Constructor
}

public Model(String name, String time) {
this.name = name;

this.time = time;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}


}

====== Now Add Data on Firebase =====
// 10 Element Added on Firebase
public void setEntryData() {

List list = new ArrayList();
for (int i = 0; i " + Calendar.getInstance().getTimeInMillis()));
}
mChild.setValue(list); // add 10 element in list data.

}

===== Now We get The All the Data Element ===

Here mChild Object which we are declared first
  mChild.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

Log.e(TAG, dataSnapshot.toString()); // Printing your whole data

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Model post = postSnapshot.getValue(Model.class);

Log.e("FatchData","=>"+post.getName() + "=>" + post.getDate());

Log.e(TAG, "===" + dataSnapshot.getKey() + "==" + dataSnapshot.getChildrenCount() + "==" dataSnapshot.hasChildren());

}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}

});


 

==== Update the Adding Data ====

 
 public void setUpdateData() {
for (int i = 0; i < 8; i++) {
DatabaseReference updateData = mChild.child(i + "");
Map changeName= new HashMap();
changeName.put("name", "Siddharth makadiya+" + i); // this is your changes
changeName.put("time", "+" + Calendar.getInstance().getTime() + i);
changeName.updateChildren(changeName);
}
}

Offline DataBase

Firebase apps automatically handle temporary network interruptions for you. Cached data will still be available while offline and your writes will be resent when network connectivity is recovered. Enabling disk persistence allows our app to also keep all of its state even after an app restart. We can enable disk persistence with just one line of code.

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

 

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Popular Posts