【问题标题】:Load Two from Two separate firebase databases into a tableview swift将两个从两个单独的 firebase 数据库加载到 tableview swift
【发布时间】:2017-08-01 12:11:13
【问题描述】:

我正在构建一个非常简单的应用程序,人们在其中单击一个按钮,并使用 firebase 数据库记录他们单击按钮(签入)的时间。我没有问题存储签入人员的数据,然后显示签入人员的数据以及所有适当的信息。我已经包含了一张图片来显示我已经在我的 TableViewController 中显示的内容。

我不想只显示已签入的用户我还想显示尚未签入的用户。

我现在将分享一张照片,说明我的 firebase 数据库是如何设置为存储每个用户的。

这里是整个数据库的视图,显示了用户和已经签入的用户以及名称和时间戳

我还将包含我的 UITableViewController 中的代码,这样每个人都可以看到我如何只显示已经签到的人。

 import Firebase

struct checkInStruct {

let userName : String!
let hour : String!
let minutes : String!

}

class checkInDaysTableViewController: UITableViewController {


//Variables
var lastChildNameSegue = ""

var posts = [checkInStruct]()
let user = FIRAuth.auth()?.currentUser

override func viewDidLoad() {
    super.viewDidLoad()

    let databaseRef = FIRDatabase.database().reference()

    let userID = user?.uid

    databaseRef.child("users").child("\(userID!)").child("checkins").child("\(lastChildNameSegue)").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in


        let userNameSnapValue = snapshot.value as? NSDictionary
        let userName = userNameSnapValue?["userName"] as? String


        let hourSnapValue = snapshot.value as? NSDictionary
        let hour = hourSnapValue?["hour"] as? String

        let minutesValue = snapshot.value as? NSDictionary
        let minutes = minutesValue?["minutes"] as? String

        print(userName!)
        print(hour!)
        print(minutes!)

        self.posts.insert(checkInStruct(userName: userName, hour: hour, minutes: minutes) , at: 0)
        self.tableView.reloadData()


    })

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")

    let userName = posts[indexPath.row].userName
    let hour = posts[indexPath.row].hour
    var minute = posts[indexPath.row].minutes

    let minuteInt = Int(minute!)

    if(minuteInt! >= 0 && minuteInt! <= 9){
        let minuteWithZero = "0\(minuteInt!)"
        minute = minuteWithZero
    }

    let label1 = cell?.viewWithTag(1) as! UILabel
    label1.text = "\(userName!)"

    let label2 = cell?.viewWithTag(2) as! UILabel
    label2.text = "Checked In At \(hour!):\(minute!)"

    return cell!
}



}

我需要做什么才能加载所有用户的数据并以红色突出显示尚未签到的任何人?

【问题讨论】:

  • 如果我做对了,您想制作一个应用程序,其中所有用户“Check_Ins”的状态对您的每个用户都是可见的,对吗?还要用文本格式的实际 JSON 更新您的问题,而不是它的快照。
  • 基本上是签到列表我要显示签到的人和时间。在同一个列表中,我想向尚未检查它的人展示@Dravidian
  • 在我看到您的整个(相关 - 架构)JSON 树之前,我无法帮助您。
  • 你好@Dravidian 我已经上传了一张展示整棵树的照片希望你现在可以提供帮助

标签: ios swift uitableview firebase swift3


【解决方案1】:

你必须改变你的 JSON 树结构

Users : {

   userID_1 : {
      userName : John Joe,
      checkInID : UNIQUE_ID_1; 
         },  

   userID_2 : {
      userName : Meghan,
      checkInID : UNIQUE_ID_2; 
         } , 

   userID_3 : {
      userName : Maria,
      checkInID : UNIQUE_ID_3; 
         } 

      },

CheckIn_List : {

     UNIQUE_ID_1 : true;  // This id has checked in
     UNIQUE_ID_2 : true;  // This id has checked in
     UNIQUE_ID_3 : false; // This id has not yet checked in..

          },
CheckIn_User_Info : {

     UNIQUE_ID_1 : {
      userName : John Joe,
      time : //time; 
         }  

   UNIQUE_ID_2 : {
      userName : Meghan,
      time : //time; 
         }  

   UNIQUE_ID_3 : {
      userName : Maria,
      time : //time; 
         } 
      }

你想遵循的程序:-

  • 如果您要对应用程序的每个用户进行身份验证,那么您已经有一个 user_ID,那么您只需担心可以用作 Firebase server Timestamp 的签入 ID。

    var checkinID = FIRServerValue.timestamp()
    
  • 如果您没有对您的用户进行身份验证;跳过您仍然需要 checkinID

  • 的第一部分
  • 将这些附加到安全规则中

     "Users" : {
    
         ".write" : "auth != null && auth.uid === $uid",  // Will allow only the user to make any change within its node and no-one else
         ".read": "auth != null && auth.uid === $uid"
         },
    
      "CheckIn_User_Info" : {
          ".write" : "auth != null", 
          ".read" : "auth != null"
         },
       "CheckIn_List": {
          ".write" : "auth != null", 
          ".read" : "auth != null"
         }
    
  • 我为 checkIn id 使用了不同的唯一 id,因为将来您可能想添加一些您可能想对其他用户隐藏的用户详细信息,这样就解决了这个问题

【讨论】:

  • 我们很清楚所有这些用户都在一个帐户下,所以我不需要担心身份验证。而且我们没有办法使用我已经拥有的 JSON 树吗? @Dravidian
【解决方案2】:

我认为你应该这样设计你的数据库。

root -&gt; users

"users": [
    "{id_user_A}": {
        "name": A,
        "checkedin": [
            "{id_checkedin_1}": {
               ...
             },
            "{id_checkedin_2}": {
               ...
             } 
        ]
    },
    "{id_user_B}": {
        "name": B,
        "checkedin": []
     }
]

并获取所有用户,使用checkedin 属性检查每个用户以了解哪个用户签入。

【讨论】:

  • 是的,但是我将如何处理多个签到和所有天的时间值?
  • 所以,让checkedin 是数组值
  • 你能想到不把它存储在用户名下做这个吗?
  • 你好@luan-tran 我已经更新了我的帖子希望你现在可以提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多