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

Supercharge your Firebase App with Google Cloud Platform

sararob
August 03, 2016

Supercharge your Firebase App with Google Cloud Platform

sararob

August 03, 2016
Tweet

More Decks by sararob

Other Decks in Programming

Transcript

  1. 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)
  2. That’s a lot of products! Let’s focus on four Backed

    by Google Cloud Storage Export raw data to BigQuery
  3. Source: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis

    non erat sem Synchronize data across devices in milliseconds
  4. // 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
  5. // 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
  6. 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
  7. 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
  8. // 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
  9. // 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
  10. 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
  11. 28 App Development @SRobTweets Firebase Analytics + BigQuery Export your

    Firebase Analytics data to BigQuery for custom analysis Android data iOS data
  12. 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... ] } ] }
  13. 36 App Development @SRobTweets How will we store our data?

    User UID Image ID Vision API response object
  14. 37 App Development @SRobTweets CocoaPods Quickstart // Podfile pod 'Firebase/Storage'

    pod 'Firebase/Analytics' // ViewController.swift import Firebase import FirebaseStorage import FirebaseAnalytics
  15. // 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
  16. // 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
  17. // 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
  18. Google Cloud Platform ‹#› Google Cloud Platform Compute Connectivity Big

    Data & ML Storage Developer Tools Mobile Management
  19. 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