gpt4 book ai didi

swift - 如何通过快照将时间戳(在 firebase Double 或 String 中的 neSTLed)检索为 int 以在 if 语句中使用?

转载 作者:行者123 更新时间:2023-11-30 10:34:27 24 4
gpt4 key购买 nike

这个问题看似简单,其实有点复杂。这是相关的数据库结构:

"people" : {
"q3up6CyAe5PoEAooJwCclOGGcZd2" : {
"peopleWhoLike2" : {
"NMNQYJ5z64fATK2MMs7m0ggHl0k2" : 1571089433551

我们对 1571089433551 时间戳感兴趣。第一个用户 ID (q3up6CyAe5PoEAooJwCclOGGcZd2) 是当前用户喜欢的用户。当前用户是 NMNQYJ5z64fATK2MMs7m0ggHl0k2

当当前时间减去 1571089433551 大于 8000 秒时,我们要编写一个 if 语句。那部分我可以做。问题不是为此。它是关于如何通过快照获取 1571089433551 作为 int

这就是时间戳的制作方式:

      ref.child("people").child(self.postID).observeSingleEvent(of: .value, with:  {(snapshot) in
if let people1 = snapshot.value as? [String: AnyObject] {

let current = Auth.auth().currentUser!.uid
let p3 = ServerValue.timestamp()as! [String : Any]
let updateLikes: [String: Any] = [current: p3]

ref.child("people").child(self.postID).child("peopleWhoLike2").updateChildValues(updateLikes, withCompletionBlock: { (error, reff) in .....

这是我尝试将其用于 IF 语句的地方

          let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid

refArtists = Database.database().reference().child("people").child(self.postID).child("peopleWhoLike2")

refArtists.observe(DataEventType.value, with: {snapshot in

---------


let secondsInDay = 86400

if (Int(-date.timeIntervalSinceNow) < secondsInDay){

上面的 ------ 部分是我需要弄清楚的。如何读取 Firebase 快照并获取 int 形式的 1571089433551。

最佳答案

这是一个字面答案 - 我怀疑可能还有更多内容,因此我可以根据需要进行更新。

从与您类似的结构开始

people
uid_0 //the user whom the current user liked,
peopleWholike2
uid_1: 123456 //the current user with a timestamp

这是直接获取时间戳的一种方法:123456。我添加了一些额外的变量,以便您可以看到指向我的 Firebase 的路径和 self.ref 。

func readChildTimestamp() {
let peopleRef = self.ref.child("people")
let thisPersonRef = peopleRef.child("uid_1")
let peopleThatLikeThisPersonRef = thisPersonRef.child("peopleWhoLike2")
peopleThatLikeThisPersonRef.observeSingleEvent(of: .childAdded, with: { snapshot in
let personUidKey = snapshot.key
let timestamp = snapshot.value as? Int ?? 0
print(personUidKey, timestamp)
})
}

在这种情况下,我利用了 .childAdded,因为它在与 .observeSingleEvent 一起使用时只会读取第一个节点。在您的问题中,您正在寻找特定的时间戳,因此这可以做到。

编辑:

根据一些附加信息,我们需要扩展这个答案。看来 peopleWhoLikeMe2 节点内可能有多个子节点,如下所示

people
uid_0
peopleWholike2
uid_1: 123
uid_2: 456
uid_3: 789

假设 uid_3 的用户登录并希望从 uid_0 的节点读取其时间戳。这是代码

func readChildTimestamp() {
let myUid = "uid_3" //this is the currently logged in user
let peopleRef = self.ref.child("people")
let thisPersonRef = peopleRef.child("uid_0")
let peopleThatLikeThisPersonRef = thisPersonRef.child("peopleWhoLike2")
let myRef = peopleThatLikeThisPersonRef.child(myUid)
myRef.observeSingleEvent(of: .value, with: { snapshot in
let personUidKey = snapshot.key
let timestamp = snapshot.value as? Int ?? 0
print(personUidKey, timestamp)
})
}

结果是

uid_3 789

关于swift - 如何通过快照将时间戳(在 firebase Double 或 String 中的 neSTLed)检索为 int 以在 if 语句中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com