Share
Embed
Firebase Essentials For Android
Erik Jhordan Rey
May 16, 2016
Transcript
47 slides
-
Erik Jhordan González Reyes Android developer "Strong believer in software
craftsmanship, SOLID, clean code and testing." -
Talk Schedule • Firebase Guide for android • Android +
Firebase Sample • Answers & Questions -
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. -
@Override public void onCreate() { super.onCreate(); Firebase.setAndroidContext(this); // other setup
code } Firebase setup firebase on android -
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": { ... } } } -
Firebase These types correspond to the types available in JSON.
•String •Boolean •Long •Double •Map<String, Object> •List<Object> -
Firebase Creating a Firebase database Reference Firebase rootRef = new
Firebase("https://docs-examples.firebaseio.com/web/data"); -
Firebase Ways to Save Data four methods for writing data
to the Firebase database. • setValue() • updateChildren() • push() • runTransaction() -
Firebase setValue() Saves data to the specified Firebase reference, replacing
any existing data at that path. Example -Firebase Reference -Object -save data using setValue() -
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 -
Firebase Firebase alanRef = ref.child("users").child("alanisawesome"); User alan = new User("Alan
Turing", 1912); alanRef.setValue(alan); Saving Data -
Firebase { "users": { "alanisawesome": { "birthYear": "1912", "fullName": "Alan
Turing" } } } Result -
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); -
Firebase Result { "users": { "alanisawesome": { "birthYear": "1912", "fullName":
"Alan Turing", "nickname": "Alan The Machine", } } } -
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); -
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" } } } -
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."); } } }); -
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); -
Firebase Result { "posts": { "-JRHTHaIs-jNPLXOQivY": { "author": "gracehop", "title":
"Announcing COBOL,Programming Language" }, "-JRHTHaKuITFIhnj02kE": { "author": "alanisawesome", "title": "The Turing Machine" } } } -
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()); } }); -
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; } } -
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()); } }); -
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 -
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 -
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 -
Firebase • FirebaseUI (UI Bindings) https://github.com/firebase/firebaseui-android • GeoFire (Geo Location)
https://github.com/firebase/geofire-java Libraries -
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 -
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 -
Erik Jhordan González Reyes Android Developer +Erik Jhordan Rey Caffrey
@ErikJhordan_Rey erikcaffrey erikcaffrey.github.io