[英]How would I implement friends relationship between users in Firebase Swift?
如何快速使用Firebase來實現用戶之間的朋友關系?
以下是我當前的DataService.swift文件。
import Foundation
import Firebase
let DB_BASE = Database.database().reference()
class DataService {
// creates instance of the class inside itself
static let instance = DataService()
private var _REF_BASE = DB_BASE
private var _REF_USERS = DB_BASE.child("users")
var REF_BASE: DatabaseReference {
return _REF_BASE
}
var REF_USERS: DatabaseReference {
return _REF_USERS
}
func createDBUser(uid: String, userData: Dictionary<String,Any>) {
REF_USERS.child(uid).updateChildValues(userData)
}
}
謝謝 :)
不確定這是一個非常完整的答案,但可能會讓您朝正確的方向前進
假設我們在Firebase中有三個用戶,並且有一個數據存儲在/ users節點中。 拉里(uid_0)有兩個朋友,萌和卷毛
users
uid_0
name: "Larry"
friends:
uid_1: true
uid_2: true
uid_1
name: "Moe"
uid_2
name: "Curly"
編寫friends節點的代碼是這樣的
let usersRef = fbRed.child("users")
let uid0Ref = usersRef.child("uid_0")
let friendsRef = uid0Ref.child("friends")
friendsRef.child("uid_1").setValue(true)
friendsRef.child("uid_2").setValue(true)
那是非常冗長的,可以壓縮,但是為了便於閱讀而保留。
請注意, 如果將來要執行查詢或其他功能,這可能不是最佳解決方案。
每個用戶應具有通過其uid 引用其他用戶的列表。 不要將整個朋友用戶數據對象存儲在每個用戶下。 這將極大地膨脹您的數據庫。
users ->
"uid1" ->
friendUids ->
0 ->
"uid2"
1 ->
"uid3"
"uid2" ->
friendUids ->
0 ->
"uid1"
"uid3" ->
friendUids ->
0 ->
"uid1"
假設您的用戶具有從Firebase處理JSON的靜態解碼功能,那么檢索用戶數據所需要做的就是:
extension DataService {
func requestUserWithUid(_ uid: String, completion: @escaping (User?) -> Void) {
REF_USERS.child(uid).observeSingleEvent(of: .value) { snapshot in
completion(User.decode(snapshot.value))
}
}
func requestFriendsForCurrentUser(completion: @escaping ([User?]?) -> Void) {
var friends = [User?]()
guard let uid = Auth.auth().currentUser?.uid else {
completion(.none)
return
}
requestUserWithUid(uid) { user in
guard let user = user else {
completion(.none)
return
}
for friendUid in user.friendUids {
requestUserWithUid(friendUid) { friend in
friends.append(friend)
}
}
completion(friends)
}
}
}
這只是許多實現方式的有限示例。 假設您的用戶數據對象具有名稱,並且我們想顯示朋友名稱的列表。
class FriendView: UIView, UITableViewDataSource {
fileprivate var friends = [User?]()
fileprivate let tableView = UITableView(frame: .zero, style: .plain)
// ----------------------------------------------------------------------------
// MARK: UIView
// ----------------------------------------------------------------------------
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(tableView)
tableView.frame = frame
tableView.dataSource = self
loadFriends()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder :) has not been implemented")
}
// ----------------------------------------------------------------------------
// MARK: Private
// ----------------------------------------------------------------------------
fileprivate func loadFriends() {
DataService.instance.requestFriendsForCurrentUser() { friends in
if let friends = friends {
self.friends = friends
self.tableView.reloadData()
}
}
}
// ----------------------------------------------------------------------------
// MARK: UITableViewDataSource
// ----------------------------------------------------------------------------
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = friends[indexPath.row]?.name
return cell
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.