Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active March 6, 2016 09:43
Show Gist options
  • Save KentarouKanno/f14a77b56cfc93ba7f59 to your computer and use it in GitHub Desktop.
Save KentarouKanno/f14a77b56cfc93ba7f59 to your computer and use it in GitHub Desktop.
Firebase Sample

Firebase Sample

Firebase BaaS( Backend as a Service)

import UIKit

class ViewController: UIViewController {
    
    var myRootRef:Firebase = Firebase(url:"https://FirabaseSample.firebaseio.com/Users")
    
    
    @IBAction func write(sender: AnyObject) {
        myRootRef.setValue([["ID": [["email": "ooo@gmail.com"],["name": "aaaaa"]]],["ID": [["email": "000@gmail.com"],["name": "bbbb"]]]])
    }

    @IBAction func read(sender: AnyObject) {
        
        var datas: [(email:String, name: String)] = []
        
        myRootRef.observeEventType(.Value, withBlock: { snapshot in
            
            if let values = snapshot.value as? [[String: [[String: String]]]] {
                for value in values {
                    if let ids = value["ID"], let email = ids[0]["email"], let name = ids[1]["name"] {
                        datas.append((email, name))
                    }
                }
            }
            
            print(datas[0].email) // ooo@gmail.com
            print(datas[0].name)  // aaaaa
            print(datas[1].email) // 000gmail.com
            print(datas[1].name)  // bbbb
        })
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Firebase管理画面でのデータ構造 Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment