Basic Reading Material from Source
https://firebase.google.com/docs/database/android/start/
https://firebase.google.com/docs/reference/node/firebase.database.Reference
2. Push()
Generates a new child location using a unique key and returns its
At the client side by chronological order. Server try to compensate by sending realtime to client before push. However, correction will only take effect if client has connect to Firebase. Hence the solution is to use ServerValue.TIMESTAMP .
https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html
Write Data:
setvalue()
Update Data in Realtime:
addValueEventListener (2 methods, onDataChange and onCancelled).
From onDataChange, use dataSnapshot.getValue.
How to save Data? (Check if input is not null, using push().getkey() to save to unique identifier)
https://firebase.google.com/docs/database/android/start/
Location of Data:
A
Reference
represents a specific location in your Database.
You can reference the root or child location in your Database by calling
firebase.database().ref()
or firebase.database().ref("child/path")
.
Methods:
1.child (path)
Gets a
Reference
for the location at the specified relative path.
The relative path can either be a simple child name (for example, "ada") or a deeper slash-separated path (for example, "ada/name/first").
returns non-null firebase.database.Reference, a child location
2. Push()
Generates a new child location using a unique key and returns its
Reference
. At the client side by chronological order. Server try to compensate by sending realtime to client before push. However, correction will only take effect if client has connect to Firebase. Hence the solution is to use ServerValue.TIMESTAMP .
https://firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html
Write Data:
setvalue()
FirebaseDatabase database = FirebaseDatabase.getInstance().getReference("message").setValue("Hello, World!");
Update Data in Realtime:
addValueEventListener (2 methods, onDataChange and onCancelled).
From onDataChange, use dataSnapshot.getValue.
myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { String value = dataSnapshot.getValue(String.class); } @Override public void onCancelled(DatabaseError error) { Log.w(TAG, "Failed to read value.", error.toException()); } });
How to save Data? (Check if input is not null, using push().getkey() to save to unique identifier)
From Video, data structure should be kept flat. To do so, 2 tables can be linked by the userid. Hence, when adding UserInformation, ID is included.
String name = ename.getText().toString();
String add = eaddress.getText().toString();
if(!TextUtils.isEmpty(name))
{
String id = myRef.push().getKey();
UserInformation userInformation = new UserInformation(name,add,id);
myRef.child(id).setValue(userInformation);
Toast.makeText(this,"User Added",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this,"Add a name",Toast.LENGTH_LONG).show();
}
TextUtilis (class)
isEmpty():Returns true if the string is null or 0-length. https://developer.android.com/reference/android/text/TextUtils
TextUtils.isEmpty() Vs String.isEmpty()
TextUtils.isEmpty() return boolean value regardless of null/length is 0. Whereas String.isEmpty() only return true if only length is 0, otherwise will result in nullpointerexception.
Do take note of the following from StackOverflow:
- If you are adding values in firebase database manually, the values will be in long data type; for example:
- If you are adding values by commands, the values will be saved in the database as string.
Solution:
When you are getting the values from database, save the values in default data type. Then, when you want to reuse the value change it into the string by using the toString() method.
Read DataDatasnapshot:
A DataSnapshot instance contains data from a Firebase Database location. Any time you read Database data, you receive the data as a DataSnapshot.
DataSnapshots are passed to the methods in listeners that you attach withaddValueEventListener(ValueEventListener)
,addChildEventListener(ChildEventListener)
, oraddListenerForSingleValueEvent(ValueEventListener)
.
They are efficiently-generated immutable copies of the data at a Firebase Database location.
They can't be modified and will never change. To modify data at a location, use aDatabaseReference
reference (e.g. withsetValue(Object)
).
public T getValue (Class<T> valueType
In short, getValue required a public constructor with no default argument and defined getter.to use it, e.g. class name: messageMessage m = snapshot.getValue(Message.class);This method is used to marshall the data contained in this snapshot into a class of your choosing. The class must fit 2 simple constraints:
- The class must have a default constructor that takes no arguments
- The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized
ReturnsAn instance of the class passed in, populated with the data from this snapshot, or null if there is no data at this location.
class Message { private String author; private String text; private Message() {} public Message(String author, String text) { this.author = author; this.text = text; } public String getAuthor() { return author; } public String getText() { return text; } } // Later Message m = snapshot.getValue(Message.class);
https://www.youtube.com/watch?v=2duc77R4Hqw
ValueEventListener
Classes implementing this interface can be used to receive events about data changes at a location. Attach the listener to a location useraddValueEventListener(ValueEventListener)
.
https://firebase.google.com/docs/reference/android/com/google/firebase/database/ValueEventListener
Comments
Post a Comment