Swift Firebase TableView数据-DataEventType.value

时间:2018-08-31 23:22:02

标签: ios swift firebase firebase-realtime-database

我正在“跟着”打电话给孩子,正在查看已登录用户的UID是否存在并且是否有已登录用户关注的另一个用户的孩子。

我正在将登录用户关注的用户打印到表格视图中。第一个问题是我的代码,因为我知道在彼此之间进行两个Firebase调用是一种不好的做法,因此我需要有人教我一个更好的方法。由于代码不正确,当我取消关注另一个用户并返回到该标签时,该标签显示了所关注的已登录用户列表,该页面将显示此信息(下图)。当登录的用户没有人关注时,它应该只显示“对不起!”。文字,但仍保留用户关注的对象。需要有人教我一种进行此类Firebase调用的更好方法。下面是代码和firebase JSON堆栈图像...在firebase JSON堆栈图像中,扩展的UID是登录用户,而子级是登录用户关注的另一个用户。我需要一种更好的方法来调用和提取此信息,我只是不知道如何做。

enter image description here

enter image description here

func getFollowingData() {

    Database.database().reference().child("following").child(uid!).observe(DataEventType.value, with: { (snapshot) in
        if snapshot.exists() {
            print("Got Snapshot")
            Database.database().reference().child("following").child(self.uid!).observe(.childAdded, with: { (snapshot) in
                if snapshot.exists() {
                    print(snapshot)
                    let snapshot = snapshot.value as? NSDictionary
                    self.listFollowing.append(snapshot)
                    self.followingTableView.insertRows(at: [IndexPath(row:self.listFollowing.count-1,section:0)], with: UITableViewRowAnimation.automatic)
                    self.followingTableView.backgroundView = nil
                }
            })
        } else {
            print("No Snapshot")
            self.followingTableView.backgroundView = self.noDataView
        }
    })
}

1 个答案:

答案 0 :(得分:0)

弄清楚了,只需要像以前在其他提要上一样做就可以了。

import UIKit
import Firebase

class BusinessFollowing: UITableViewController {

@IBOutlet var noDataView: UIView!
@IBOutlet var followingTableView: UITableView!

var yourFollowing = [Information]()

var listFollowing = [NSDictionary?]()
var databaseRef = Database.database().reference()
let uid = Auth.auth().currentUser?.uid


var loggedInUser = Auth.auth().currentUser
var loggedInUserData:NSDictionary?

var following = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.followingTableView.backgroundView = nil


}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.followingTableView.reloadData()
    self.yourFollowing.removeAll()
    self.following.removeAll()

    getFollowingData()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "following" {
        // gotta check if we're currently searching
            if let indexPath = followingTableView.indexPathForSelectedRow {
                let user = listFollowing[indexPath.row]
                let controller = segue.destination as? ExploreBusinessProfileSwitchView
                controller?.otherUser = user
            }
    }
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return self.yourFollowing.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BusinessFollowingCell
    let following = yourFollowing[indexPath.row]

    let businessName = following.businessName
    let businessStreet = following.businessStreet
    let businessCity = following.businessCity
    let businessState = following.businessState

    cell.businessName.text = businessName
    cell.businessStreet.text = businessStreet
    cell.businessCity.text = businessCity
    cell.businessState.text = businessState

    // cell.businessName?.text = self.listFollowing[indexPath.row]?["businessName"] as? String
    // cell.businessStreet?.text = self.listFollowing[indexPath.row]?["businessStreet"] as? String
    // cell.businessCity?.text = self.listFollowing[indexPath.row]?["businessCity"] as? String
    // cell.businessState?.text = self.listFollowing[indexPath.row]?["businessState"] as? String

    return cell
}

func getFollowingData() {
    self.yourFollowing.removeAll()
    self.following.removeAll()
    self.followingTableView.reloadData()

    Database.database().reference().child("Businesses").child((loggedInUser?.uid)!).child("following").observe(.value, with: { snapshot in
        if snapshot.exists() {
            MBProgressHUD.showAdded(to: self.view, animated: true)
            let databaseRef = Database.database().reference()
            databaseRef.child("Businesses").queryOrderedByKey().observeSingleEvent(of: .value, with: { (usersSnapshot) in
                let users = usersSnapshot.value as! [String: AnyObject]
                for (_, value) in users {
                    if let userID = value["uid"] as? String {
                        if userID == Auth.auth().currentUser?.uid {
                            print(value)
                            if let followingUsers = value["following"] as? [String : String] {
                                for (_,user) in followingUsers {
                                    self.following.append(user)
                                }
                            }
                            databaseRef.child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { (postsSnapshot) in
                                let posts = postsSnapshot.value as! [String: AnyObject]

                                for (_, post) in posts {
                                    for (_, postInfo) in post as! [String: AnyObject] {
                                        if let followingID = postInfo["uid"] as? String {
                                            for each in self.following {
                                                if each == followingID {
                                                    guard let uid = postInfo["uid"] as! String? else {return}
                                                    guard let name = postInfo["businessName"] as! String? else {return}
                                                    guard let address = postInfo["businessStreet"] as! String? else {return}
                                                    guard let state = postInfo["businessState"] as! String? else {return}
                                                    guard let city = postInfo["businessCity"] as! String? else {return}

                                                    self.yourFollowing.append(Information(uid: uid, businessName: name, businessStreet: address, businessCity: city, businessState: state))
                                                }

                                                self.followingTableView.backgroundView = nil
                                                self.followingTableView.reloadData()
                                            }
                                        }
                                    }
                                }
                                MBProgressHUD.hide(for: self.view, animated: true)
                            }) { (error) in
                                print(error.localizedDescription)
                            }
                        }
                    }
                }

            })
        } else {
            print("Not following anyone")
            self.followingTableView.backgroundView = self.noDataView
            MBProgressHUD.hide(for: self.view, animated: true)
        }
    })

}

}