gpt4 book ai didi

ios - 如何在我的 iOS 应用程序中检索嵌套的 firebase 数据?

转载 作者:行者123 更新时间:2023-11-29 13:52:33 25 4
gpt4 key购买 nike

我在 firebase 中嵌套了数据库。我必须从 firebase 读取数据到我的应用程序,但在这一点上数据有点复杂,所以我无法正确检索。我必须使用“adres”、“isim”、“tarih”和“urunler”节点,我也可以使用 childSnapshot 和 for 循环获取 adres 数据,但我失去了秩序。你能帮我找回这种数据吗与订单?这是我的数据库 json:

"orders" : {
"kullanicilar" : {
"D5wmYp3YyegUOwtpVeqa0IoKnqA2" : {
"-Lu3Fl2QnSFa5vhrKOzv" : {
"Tshirt" : 4,
"adres" : {
"siparis alinma adresi" : "Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958",
"siparis edilme adresi" : "Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958"
},
"isim" : {
"username" : "Testçi"
},
"tarih" : {
"teslim alinma tarihi" : {
"alinma saati" : "10:00 - 11:00",
"alinma tarihi" : "19/11/2019"
},
"teslim edilme tarihi" : {
"alinma tarihi" : "21/11/2019",
"teslim saati" : "18:00 - 19:00"
}
},
"toplam tutar" : {
"toplam paket tutari" : 55.6
},
"urun adetleri" : {
"adetler" : [ 4 ]
},
"urunler" : {
"Tshirt" : 4
}
},
"-Lu3G-97OfWJB1u34gJ8" : {
"Sweatshirt" : 3,
"Tshirt" : 4,
"adres" : {
"siparis alinma adresi" : "Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958",
"siparis edilme adresi" : "Bostancı - Yan Yol - Yan apt no:4 - Kadıköy - 7 - 23 - 29.103910631539957 - 40.96400403065958"
},
"isim" : {
"username" : "Testçi"
},
"tarih" : {
"teslim alinma tarihi" : {
"alinma saati" : "18:00 - 19:00",
"alinma tarihi" : "22/11/2019"
},
"teslim edilme tarihi" : {
"alinma tarihi" : "27/11/2019",
"teslim saati" : ""
}
},
"toplam tutar" : {
"toplam paket tutari" : 99.1
},
"urun adetleri" : {
"adetler" : [ 4, 3 ]
},
"urunler" : {
"Sweatshirt" : 3,
"Tshirt" : 4
}
}
},

我在这里尝试从 firebase 获取数据,然后将这些数据存储到数组中。这是我尝试检索的代码:

func firebaseConnection() {
ref = Database.database().reference()
ref?.child("orders").child("kullanicilar").observe(.childAdded, with: { (datasnapshot) in

let imageD = datasnapshot
self.userId = imageD.key

self.ref?.child("orders").child("kullanicilar").child(self.userId).observe(.childAdded, with: { (snapshot) in


// ADRESLERI TUTAN KOD BURASIDIR.. ADRESLERI ALIR VE HEM ARRAY HEM DE SOZLUGE KAYIT EDER..
let adresChild = snapshot.childSnapshot(forPath: "adres")
for adr in adresChild.children {
let adresSnap = adr as! DataSnapshot
self.adresValuesArray.append(adresSnap.value! as! String)

}

let tarihChild = snapshot.childSnapshot(forPath: "tarih").childSnapshot(forPath: "teslim alinma tarihi")
for trh in tarihChild.children {
let tarihSnap = trh as! DataSnapshot
self.tarihValuesArray.append(tarihSnap.value! as! String)
}

let tarihChildteslim = snapshot.childSnapshot(forPath: "tarih").childSnapshot(forPath: "teslim edilme tarihi")
for trhts in tarihChildteslim.children {
let tarihTeslimSnap = trhts as! DataSnapshot
self.tarihTeslimValuesArray.append(tarihTeslimSnap.value! as! String)

}


let urunlerChild = snapshot.childSnapshot(forPath: "urunler")

for urn in urunlerChild.children {
let urunSnap = urn as! DataSnapshot
//self.urunlerArray.updateValue(urunSnap.value! as! Int, forKey: urunSnap.key as! String)

self.urunlerArray.append(urunSnap)
print(self.urunlerArray)



}


})
})

}

最佳答案

我不知道这些节点名称是什么意思 - 可能是土耳其语,所以我使用的是类似的英文版本。

这是你的结构

orders
all_users
uid_0
TShirt: 4
addresses:
address_0: ""
address_1: ""
delivery_dates
delivery_0
delivery_date: "19/11/2019"
delivery_time: "10:00 - 11:00"
delivery_1
delivery_date: "21/11/2019"
delivery_time: "18:00 - 19:00"
name
username: "some username"

我们正在使用 .childAdded 来读取 all_users 中的每个 uid 节点 - 这将一次迭代一个节点,以便访问快照中返回的子数据。

下面是读取每个用户节点并打印出所有数据的代码。

func readUserDeliveryInfo() {
let ordersRef = self.ref.child("orders")
let allUsersRef = ordersRef.child("all_users")
allUsersRef.observe(.childAdded, with: { snapshot in
let userId = snapshot.key
print("data for user: \(userId)")

let tshirt = snapshot.childSnapshot(forPath: "TShirt").value as? Int ?? 0
print("t-shirt: \(tshirt)")

let allAddresses = snapshot.childSnapshot(forPath: "addresses")
let addressArray = allAddresses.children.allObjects as! [DataSnapshot]
for addressSnap in addressArray {
let addressKey = addressSnap.key
let address = addressSnap.value as! String
print("address: \(addressKey) \(address)")
}

let allDeliveries = snapshot.childSnapshot(forPath: "delivery_dates")
for delivery in allDeliveries.children.allObjects as! [DataSnapshot] {
let deliveryId = delivery.key
let deliveryDate = delivery.childSnapshot(forPath: "delivery_date").value as? String ?? "No date"
let deliveryTime = delivery.childSnapshot(forPath: "delivery_time").value as? String ?? "No Time"
print(deliveryId, deliveryDate, deliveryTime)
}

let nameSnap = snapshot.childSnapshot(forPath: "name")
let userName = nameSnap.childSnapshot(forPath: "username").value as? String ?? "No username"
print("username: \(userName)")
})
}

和输出

data for user: uid_0
t-shirt: 4
address: address_0 some address 0
address: address_1 some address 1
delivery_0 19/11/2019 10:00 - 11:00
delivery_1 21/11/2019 18:00 - 19:00
username: some username

一些注意事项:

Firebase DataSnapshot 非常强大,因此请尽可能多地利用它们 - 它们将保持数据有序并提供对子数据的轻松访问。

let tshirt = snapshot.childSnapshot(forPath: "TShirt")

当我确实需要遍历快照中的数据时,我将其转换为一个数组(保持顺序不同于字典),并将子数据转换为 DataSnapshots 本身。

let addressArray = allAddresses.children.allObjects as! [DataSnapshot]

关于ios - 如何在我的 iOS 应用程序中检索嵌套的 firebase 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58930050/

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