【问题标题】:Receiving a Firebase snapshot from a child with an array SWIFT从具有数组 SWIFT 的子节点接收 Firebase 快照
【发布时间】:2017-03-08 03:17:10
【问题描述】:

所以我目前正在尝试从我的 Firebase 数据库中获取数据并将其设置为自己的变量,但每个图表的子项都是特定的日期和时间 (yy.mm.dd.h.m.s)。所以我有一个数组来存储我需要的所有日期,但是在调用我的快照时我无法引用它们。我已经尝试过这两种抛出错误的方法 "(child:) Must be a non-empty string and not contain '.' '#' '$' '[' 或 ']''"

var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss   

ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath.row]).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

var postCollection = [170802120618, 170802101427]  //yy.mm.dd.hh.mm.ss  
let selection = self.postCollection[indexPath.row]
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

数据库图表大致如下:

FIR{
    users{
        uid{
            username: UserName
            Posts{
                170802120618{
                    First Name: first
                }
            }
        }
    }
}

【问题讨论】:

    标签: ios arrays swift firebase firebase-realtime-database


    【解决方案1】:

    没错。您希望子键是自动生成的哈希值。您可以使用childByAutoId() 创建这些。另外,如果我是您,我只会将该日期存储为字符串并根据需要对其进行解析。下面是一个例子:

    Posts {
       -Kebfdajksthm {
          first_name: "first",
          post_date: "yymmddhhmmss"
       }
    }
    

    【讨论】:

    • 我明白你的意思,但是我将如何引用它呢?我正在尝试为多个用户执行此操作,因此我无法使用 currentUser 或 currentChild。
    【解决方案2】:

    试试这个

    var post = [String]()
    ref.observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    self.post.append((item as AnyObject).key)
            }
        })
    

    然后你打印"post",你会得到["170802120618", "170802101427"]

    【讨论】:

      猜你喜欢
      • 2017-01-30
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 2017-09-10
      • 1970-01-01
      相关资源
      最近更新 更多