data with setValue() databaseRef.setValue("Hello everyone!") // Sync data databaseRef.observeEventType(.Value, withBlock: { snapshot in print(snapshot.value) }) Store & Sync Data 9
= 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
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
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
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
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