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

Firebase Essentials For Android

Firebase Essentials For Android

Erik Jhordan Rey

May 16, 2016
Tweet

More Decks by Erik Jhordan Rey

Other Decks in Programming

Transcript

  1. Talk Schedule • Firebase Guide for android • Android +

    Firebase Sample • Answers & Questions
  2. 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.
  3. 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": { ... } } }
  4. Firebase These types correspond to the types available in JSON.

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

    Firebase("https://docs-examples.firebaseio.com/web/data");
  6. Firebase Ways to Save Data four methods for writing data

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

    any existing data at that path. Example -Firebase Reference -Object -save data using setValue()
  8. 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
  9. 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);
  10. Firebase Result { "users": { "alanisawesome": { "birthYear": "1912", "fullName":

    "Alan Turing", "nickname": "Alan The Machine", } } }
  11. 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);
  12. 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" } } }
  13. 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."); } } });
  14. 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);
  15. Firebase Result { "posts": { "-JRHTHaIs-jNPLXOQivY": { "author": "gracehop", "title":

    "Announcing COBOL,Programming Language" }, "-JRHTHaKuITFIhnj02kE": { "author": "alanisawesome", "title": "The Turing Machine" } } }
  16. 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()); } });
  17. 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; } }
  18. 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()); } });
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. Erik Jhordan González Reyes Android Developer +Erik Jhordan Rey Caffrey

    @ErikJhordan_Rey erikcaffrey erikcaffrey.github.io