2016-09-05 55 views
-4

我已經創建了2個表格。我想選擇表朋友,我想顯示名稱從表用戶?任何人都可以幫助我例如迅速請謝謝。swift firebase一對多例子?

表用戶

id 
name 
email 

表的朋友

id 
user_id 
friend_id 
+1

歡迎堆棧溢出:-) _「請給我碼」 _是不是這個網站的工作方式。展示你自己的努力並解釋你的問題。請看[問] – JimHawkins

回答

0

你的實時數據庫應該是這個樣子。

{ 
    "users": { 
     "1": { 
      "name": "John Doe", 
      "email": "[email protected]" 
     }, 
     "2": { 
      "name": "Jane Doe", 
      "email": "[email protected]" 
     } 
    }, 
    "user-friends": { 
     "1": { 
      "2": true 
     }, 
     "2": { 
      "1": true 
     } 
    } 
} 

爲了得到約翰的朋友,我們觀察到的路徑/user-friends/1

let uid = 1 // the uid of the user we want the friends of 
let ref = FIRDatabase.database().reference().child('user-friends/\(uid)') 

ref.observeEventType(.ChildAdded, block: { snapshot in 

    let uid = snapshot.key // the uid of a user who is John's friend 
    let ref = FIRDatabase.database().reference().child('users/\(uid)') 

    ref.observeSingleEventOfType(.Value, block: { snapshot in 
     if let json = snapshot.value as? [String:AnyObject] { 
      print(json["name"]) 
     } 
    }) 
}) 
+0

非常感謝你的幫助 – user2502099