Firebase Essentials For Android

Firebase Essentials For Android

9f523401a845a29c93ff73cde4c3db2b?s=128

Erik Jhordan Rey

May 16, 2016
Tweet

Transcript

  1. Firebase Essentials For Android ¬ GDG WonderCoders, May 2016

  2. Erik Jhordan González Reyes Android developer "Strong believer in software

    craftsmanship, SOLID, clean code and testing."
  3. Talk Schedule • Firebase Guide for android • Android +

    Firebase Sample • Answers & Questions
  4. Installation & Setup

  5. dependencies { compile 'com.firebase:firebase-client-android:2.5.2+' } Firebase

  6. Firebase android { ... packagingOptions { exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE-FIREBASE.txt'

    exclude 'META-INF/NOTICE' } } If you are getting a build error complaining about duplicate files you can choose to exclude those files.
  7. <uses-permission android:name="android.permission.INTERNET" /> Firebase Add Internet Permission

  8. @Override public void onCreate() { super.onCreate(); Firebase.setAndroidContext(this); // other setup

    code } Firebase setup firebase on android
  9. Understanding Data Firebase

  10. Firebase All Firebase database data is stored as JSON objects.

    { "users": { "mchen": { "friends": { "brinchen": true }, "name": "Mary Chen", // our child node appears in the existing JSON tree "widgets": { "one": true, "three": true } }, "brinchen": { ... }, "hmadi": { ... } } }
  11. Firebase These types correspond to the types available in JSON.

    •String •Boolean •Long •Double •Map<String, Object> •List<Object>
  12. Firebase Creating a Firebase database Reference Firebase rootRef = new

    Firebase("https://docs-examples.firebaseio.com/web/data");
  13. Saving Data Firebase

  14. Firebase Ways to Save Data four methods for writing data

    to the Firebase database. • setValue() • updateChildren() • push() • runTransaction()
  15. Firebase setValue() Saves data to the specified Firebase reference, replacing

    any existing data at that path. Example -Firebase Reference -Object -save data using setValue()
  16. Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog"); Firebase Reference

  17. Firebase public class User { private int birthYear; private String

    fullName; public User() {} public User(String fullName, int birthYear) { this.fullName = fullName; this.birthYear = birthYear; } public long getBirthYear() { return birthYear; } public String getFullName() { return fullName; } } Creating an Object
  18. Firebase Firebase alanRef = ref.child("users").child("alanisawesome"); User alan = new User("Alan

    Turing", 1912); alanRef.setValue(alan); Saving Data
  19. Firebase { "users": { "alanisawesome": { "birthYear": "1912", "fullName": "Alan

    Turing" } } } Result
  20. Firebase updateChildren() If you want to write to specific children

    of a node at the same time without overwriting other child nodes. Firebase alanRef = usersRef.child("alanisawesome"); Map<String, Object> nickname = new HashMap<String, Object>(); nickname.put("nickname", "Alan The Machine"); alanRef.updateChildren(nickname);
  21. Firebase Result { "users": { "alanisawesome": { "birthYear": "1912", "fullName":

    "Alan Turing", "nickname": "Alan The Machine", } } }
  22. Firebase Using multi-path updates updateChildren() can now update values at

    multiple locations in your Firebase database at the same time. Map<String, Object> nicknames = new HashMap<String, Object>(); nickname.put("alanisawesome/nickname", "Alan The Machine"); nickname.put("gracehop/nickname", "Amazing Grace"); usersRef.updateChildren(nicknames);
  23. Firebase Result "users": { "alanisawesome": { "date_of_birth": "June 23, 1912",

    "full_name": "Alan Turing", "nickname": "Alan The Machine" }, "gracehop": { "date_of_birth": "December 9, 1906", "full_name": "Grace Hopper", "nickname": "Amazing Grace" } } }
  24. Firebase Adding a Completion Callback ref.setValue("I'm writing data", new Firebase.CompletionListener()

    { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { System.out.println("Data could not be saved. " + firebaseError. getMessage()); } else { System.out.println("Data saved successfully."); } } });
  25. Firebase push() Generates a unique ID every time a new

    child is added to the specified Firebase reference. Firebase postRef = ref.child("posts"); Map<String, String> post1 = new HashMap<String, String>(); post1.put("author", "gracehop"); post1.put("title", "Announcing COBOL, a New Programming Language"); postRef.push().setValue(post1); Map<String, String> post2 = new HashMap<String, String>(); post2.put("author", "alanisawesome"); post2.put("title", "The Turing Machine"); postRef.push().setValue(post2);
  26. Firebase Result { "posts": { "-JRHTHaIs-jNPLXOQivY": { "author": "gracehop", "title":

    "Announcing COBOL,Programming Language" }, "-JRHTHaKuITFIhnj02kE": { "author": "alanisawesome", "title": "The Turing Machine" } } }
  27. Retrieving Data Firebase

  28. Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); // Attach an listener

    to read the data at our posts reference ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { System.out.println(snapshot.getValue()); } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } });
  29. Firebase Read event types public class BlogPost { private String

    author; private String title; public BlogPost() { // empty default constructor, necessary for Firebase to be able to deserialize blog posts } public String getAuthor() { return author; } public String getTitle() { return title; } }
  30. Firebase Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts"); // Attach an listener

    to read the data at our posts reference ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { System.out.println("There are " + snapshot.getChildrenCount() + " blog posts"); for (DataSnapshot postSnapshot: snapshot.getChildren()) { BlogPost post = postSnapshot.getValue(BlogPost.class); System.out.println(post.getAuthor() + " - " + post.getTitle()); } } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } });
  31. Structuring Data Firebase

  32. Firebase Because we can nest data up to 32 levels

    deep, it's tempting to think that this should be the default structure. However, when we fetch data at a location in our database, we also retrieve all of its child nodes. Therefore, nesting data must be done with careful consideration for how the data will be read later. Use Nested Data Sparingly
  33. Understanding Security Firebase

  34. Firebase • Authentication • Authorization • Data Validation Overview

  35. User Authentication Firebase

  36. Firebase Firebase makes authentication easy. It can integrate with your

    existing login server, or authenticate users with only client-side code. It has built-in functionality for email & password, and third-party providers such as Facebook, Twitter, GitHub, and Google. Authentication
  37. Offline Capabilities Firebase

  38. Firebase 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. Firebase.getDefaultConfig().setPersistenceEnabled(true); Disk Persistence
  39. Firebase Tools Firebase

  40. Firebase • FirebaseUI (UI Bindings) https://github.com/firebase/firebaseui-android • GeoFire (Geo Location)

    https://github.com/firebase/geofire-java Libraries
  41. Firebase • Office Mover https://github.com/firebase/office-mover-5000 • Chat https://github.com/firebase/AndroidChat • Drawing

    https://github.com/firebase/AndroidDrawing • SF Vehicles https://github.com/firebase/geofire-java/tree/master/examples/SFVehicles • Login Demo https://github.com/firebase/firebase-login-demo-android Examples
  42. #Show me code Firebase

  43. Firebase https://github.com/erikcaffrey/Firebase-chat

  44. Answers & Questions

  45. Further Reading • My Personal Blog https://erikcaffrey.github.io • Firebase Documentation

    https://www.firebase.com/docs/android/ • Udacity - Firebase Essentials For Android https://www.udacity.com/course/firebase-essentials-for-android--ud009 • Github - Firebase https://github.com/firebase
  46. Erik Jhordan González Reyes Android Developer +Erik Jhordan Rey Caffrey

    @ErikJhordan_Rey erikcaffrey erikcaffrey.github.io
  47. firebase.child(“ThankYou”).setValue(“GDGWonderCoders”);