Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Firebase (Realtime Database)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Adi Nugroho Adi Nugroho
September 20, 2016

Introduction to Firebase (Realtime Database)

KelasMobile Malang Meetup 3.

2016/09/20

Avatar for Adi Nugroho

Adi Nugroho

September 20, 2016
Tweet

More Decks by Adi Nugroho

Other Decks in Programming

Transcript

  1. Realtime Database • Store • Sync • Collaboration ! •

    Work Offline ✈ • Multiplatform (Android, iOS, Web, IOT, etc) # Realtime database in Firebase ▶ NoSQL ▶ JSON Firebase uses Websocket
  2. Android • Project Gradle classpath 'com.google.gms:google-services:3.0.0' • App Gradle //

    auto add firebase analytics compile 'com.google.firebase:firebase-core:9.4.0' compile 'com.google.firebase:firebase-database:9.4.0' . . . apply plugin: 'com.google.gms.google-services'
  3. iOS • Pod # auto add firebase analytics pod 'Firebase'

    pod 'Firebase/Database' • GoogleService-Info.plist • AppDelegate init FIRApp.configure()
  4. Note • DataSnapshot is immutable ! • DataSnapshot value :

    Android HashMap or POJO (Plain Old Java Object). iOS Dictionary. • Key must be String. Value can be primitive types and Android HashMap or iOS Dictionary. • Android : Firebase handle Object " JSON. • Use @Exclude to exclude field.
  5. Note • No native support of array / list •

    Array will be saved as hashmap with integer key • When retrieved will be returned as array / list
  6. Offline Capability Note • Transaction is queued & committed ONLY

    when app is on foreground. • Show warning when data has not been saved online. • Manually handle transaction when app is restarted.
  7. Write Data • Android setValue( Object ) iOS setValue( AnyObject

    ) • Android updateChildren( HashMap<String, Object> ) iOS updateChildValues( Dictionary ) • update spesific child / children • passing null = remove the child
  8. updateChildren HashMap<String, Object> updatedChildren = new HashMap<>(); updatedChildren.put("price", "$ 21.000.000");

    HashMap<String, Object> winnerNode = new HashMap<>(); winnerNode.put("winner", "Digital Chaos"); updatedChildren.put("info", winnerNode); dotaRef.updateChildren( updatedChildren );
  9. updateChildValues let param : [String: Any] = [ "price": "$

    21.000.000", "info": [ "winner": "Digital Chaos" ] ] dotaRef.updateChildValues(param)
  10. Write Data • Android push() iOS childByAutoId() • only return

    reference to auto generated child • Child has unique ID & sorted by date • like append array • runTransaction( Transaction.Handler ) • multiple concurrent operations • keep data from being corrupted • e.g counter
  11. runTransaction upvotesRef.runTransaction(new Transaction.Handler() { @Override public Transaction.Result doTransaction(MutableData currentData) {

    Integer value = (Integer) currentData.getValue(); // must handle null if (value == null) { value = 0; } currentData.setValue( value + 1 ); return Transaction.success(currentData); //we can also abort by calling Transaction.abort() } @Override public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) { //This method will be called once with the results of the transaction. } });
  12. Write & Remove Data • all except push() have completion

    callback. • called when operation is completed dotaRef.updateChildren(updatedChildren, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) { } });
  13. Read Data • addValueEventListener • observeEventType( FIRDataEventType.Value , (snapshot) ->

    Void ) • addChildEventListener • observeEventType( FIRDataEventType.ChildChanged, (snapshot) -> Void ) • addListenerForSingleValueEvent • observeSingleEventOfType( .Value, (snapshot) -> Void )
  14. More • Query (sort, limit, filter) • FirebaseUI • FirebaseListAdapter

    • FirebaseRecyclerViewAdapter • Efficient data structuring • Firebase Auth • Offline / Disk persistance • Persistence handling • Detect connection
  15. Links • https://firebase.google.com/docs/ • https://firebase.google.com/docs/database/ • Udacity : Firebase Essential

    for Android • https://classroom.udacity.com/courses/ud009 • Legacy firebase. • Existing firebase app : • https://firebase.google.com/support/guides/firebase-android • https://firebase.google.com/support/guides/firebase-ios