Share
Embed
Transcript
43 slides
-
APP DEVELOPMENT Supercharge your Firebase app with Google Cloud Platform
Sara Robinson @SRobTweets -
2 App Development @SRobTweets Who am I? Developer Advocate, Google
Cloud Platform Sara Robinson / @SRobTweets • New York, NY • Harry Potter aficionado • Swift fan (Taylor and language) -
That’s a lot of products! Let’s focus on four Backed
by Google Cloud Storage Export raw data to BigQuery -
Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis
non erat sem Synchronize data across devices in milliseconds -
// Create a reference let databaseRef = FIRDatabase.database().referenceWithPath("messages") // Save
data with setValue() databaseRef.setValue("Hello everyone!") // Sync data databaseRef.observeEventType(.Value, withBlock: { snapshot in print(snapshot.value) }) Store & Sync Data 9 -
// Enable disk persistence FIRDatabase.database().persistenceEnabled = true // Specify data
that should be pre-fetched and kept up to date databaseRef.keepSynced(true) 10 Handling Offline Data -
FIRAuth.auth()?.signInAnonymouslyWithCompletion({ (user, error) in if (error != nil) { print(error)
} else { print(user.uid) } }) Anonymous Authentication 13 -
14 App Development @SRobTweets Getting Started on iOS - CocoaPods
// Podfile pod 'Firebase/Database' pod 'Firebase/Auth' // Terminal $ pod install $ open your-project.xcworkspace // ViewController.swift import Firebase import FirebaseDatabase import FirebaseAuth -
17 App Development 17 Firebase Storage and Analytics 02 Firebase
+ Cloud Integrations -
Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis
non erat sem Easily store and serve files and images, backed by Google Cloud Storage -
// Create a reference let storageRef = FIRStorage.storage().reference() let imageRef
= storageRef.child("images/ocean.jpg") // Upload files from data in memory let data: NSData! = … imageRef.putData(data, metadata: nil) { (metadata, error) -> Void in if (error != nil) { // Uh-oh, an error occurred! } else { // Metadata contains data on size, content-type, and download URL. let downloadURL = metadata!.downloadURL } } Uploading Files 19 -
// Anyone to read a public image if the file
is less than 100kB // Anyone can upload a public file ending in '.txt' match /public/{imageId} { allow read: if resource.size < 100 * 1024; allow write: if imageId.matches(".*\.txt"); } // Require authentication on all internal image reads match /internal/{imageId} { allow read: if request.auth != null; } Storage Security Rules 20 -
BigQuery • Fast: terabytes in seconds • Simple: SQL •
Scaleable: From bytes to petabytes • Interoperable: Java, Python, Tableau, R... • Pay only for what you use • Free monthly quota - 1 TB 27 -
28 App Development @SRobTweets Firebase Analytics + BigQuery Export your
Firebase Analytics data to BigQuery for custom analysis Android data iOS data -
// Create an analytics event FIRAnalytics.logEventWithName("pastryPurchased", parameters: [ "item_type": "cannoli",
kFIRParameterValue: "3.5" ] ) Creating an Analytics Event 29 -
32 App Development 32 32 A photo storage app with
image detection -
Cloud Vision API Request { "requests":[ { "image": { "source":
{ "gcs_image_uri": "gs://images/your-photo-uri" } }, "features": [ { "type": "LABEL_DETECTION", "maxResults": 10 }, { "type": "FACE_DETECTION", "maxResults": 10 }, // More feature detection types... ] } ] } -
36 App Development @SRobTweets How will we store our data?
User UID Image ID Vision API response object -
37 App Development @SRobTweets CocoaPods Quickstart // Podfile pod 'Firebase/Storage'
pod 'Firebase/Analytics' // ViewController.swift import Firebase import FirebaseStorage import FirebaseAnalytics -
// Create a path to upload to Firebase Storage let
imagesRef = FIRStorage.storage().referenceForURL("gs://swift-storage.appspot.com").child("imag es/\(userUid)/\(imageId).\(fileExtension)") // Upload to Firebase storage imagesRef.putData(imageData, metadata: imgMetadata) { (metadata, error) -> Void in if (error == nil) { FIRAnalytics.logEventWithName("imgUploadSuccess", parameters: [ "content_type": fileExtension, ]) let gcsUri = imagesRef.description self.createRequest(gcsUri) // Send the image to the Vision API } } Step 1: Upload images to Firebase Storage 38 -
// Get the photo metadata we want to save to
our Firebase Database let visionApiResponse: AnyObject = responses.rawValue let imageDataforFB = [ "visionResponse": dictResponses, "gcsUri": imgRef ] // Get a unique Firebase Database ref for the image and save it let userImageRef = FIRDatabase.database().referenceWithPath("\(userUid)/images") let uniqueImageRef = userImageRef.childByAutoId() uniqueImageRef.setValue(imageDataforFB) Step 2: Save photo metadata to the Firebase Database 39 -
// Listen for items added to our database at /user/images/
userImageRef.observeEventType(.ChildAdded, withBlock: { snapshot in // Get the Firebase Storage reference for the latest image added let imageUrl = snapshot.value?.objectForKey("gcsUri") as! String let storageRef = FIRStorage.storage().referenceForURL(imageUrl) // Download the image as a URL and populate our UICollectionView storageRef.downloadURLWithCompletion({ (url, error) in var currentPhoto = PhotoHolder() currentPhoto.firebaseKey = snapshot.key currentPhoto.url = url self.photoArray.append(currentPhoto) self.collectionView?.reloadData() }) }) Step 3: Listen for new images and add them to the view 40 -
Google Cloud Platform ‹#› Google Cloud Platform Compute Connectivity Big
Data & ML Storage Developer Tools Mobile Management -
42 App Development @SRobTweets Resources Firebase docs: firebase.google.com/docs [Video] Build
a cross-platform Firebase app! bit.ly/zero-to-app Vision API samples: github.com/GoogleCloudPlatform/cloud-vision Join the discussion bit.ly/gcp-slack bit.ly/gcp-github bit.ly/gcp-twitter