gpt4 book ai didi

ios - 为什么我从数据库中提取信息的函数不起作用?

转载 作者:行者123 更新时间:2023-11-28 05:37:48 24 4
gpt4 key购买 nike

我正在尝试从我的 Firebase 数据库中提取数据列表。下面是我的代码。在我的 viewDidLoad(); 中调用了这两个函数,但是,即使我的数据库已完全填充,print(theLikeArray) 仍返回空括号。

下面是我目前使用的代码。第一个函数用于从数据库中检索所有信息,而第二个函数用于填充数据库。第二个函数在第一个函数之前被调用。

func retriveDiscounts() {
let likesDB = Database.database().reference().child("Discounts")
likesDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String , String>
let businessID = Int(snapshotValue["BusinessID"]!)
let businessName = snapshotValue["businessName"]!
let DateNumber = Int(snapshotValue["DateNumber"]!)
let theDeal = snapshotValue["theDeal"]!
let phoneNumber = snapshotValue["PhoneNumberText"]!
let imageName = snapshotValue["ImageName"]!
let dateText = snapshotValue["DateText"]!
let phoneNumberInteger = Int(snapshotValue["phoneNumberInteger"]!)
let companyLogo = snapshotValue["companyLogo"]!
let r1 = Int(snapshotValue["r1"]!)
let r2 = Int(snapshotValue["r2"]!)
let r3 = Int(snapshotValue["r3"]!)
let classification = snapshotValue["classification"]!
let allTheLikes = likeclass()
allTheLikes.discountID = businessID!
allTheLikes.businessName = businessName
allTheLikes.dateApplied = DateNumber!
allTheLikes.theDeal = theDeal
allTheLikes.phoneNumber = phoneNumber
allTheLikes.imageName = imageName
allTheLikes.dateText = dateText
allTheLikes.numberNumber = phoneNumberInteger!
allTheLikes.companylogo = companyLogo
allTheLikes.r1 = r1!
allTheLikes.r2 = r2!
allTheLikes.r3 = r3!
allTheLikes.classification = classification
self.theLikeArray.append(allTheLikes)
}
print(theLikeArray)
}



func updateLikeDatabase(){
for i in 0...allDiscounts.list.count-1{
let likesDB = Database.database().reference().child("Discounts")
let likeDictionary = ["BusinessID": "\(i)","businessName":"\(allDiscounts.list[i].businessName)","DateNumber": "\(allDiscounts.list[i].dateApplied)","theDeal": "\(allDiscounts.list[i].theDeal)" ,"PhoneNumberText": "\(allDiscounts.list[i].phoneNumber)","ImageName": "\(allDiscounts.list[i].imageName)","DateText": "\(allDiscounts.list[i].dateText)" ,"phoneNumberInteger": "\(allDiscounts.list[i].numberNumber)","companyLogo": "\(allDiscounts.list[i].companylogo)","r1": "\(allDiscounts.list[i].r1)" ,"r2": "\(allDiscounts.list[i].r2)","r3": "\(allDiscounts.list[i].r3)","classification": "\(allDiscounts.list[i].classification)"] as [String : String]
likesDB.child("\(allDiscounts.list[i].businessName)").setValue(likeDictionary) {
(error, reference) in
if error != nil{
let alert = UIAlertController(title: "Error", message: "There was an error registering your like. Reconnect online and try again", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert,animated: true)
// self.showerror()
}
else{
print("Message Saved Successfully")
}
}
}
}

最佳答案

可能必须从 Firebase 服务器下载数据。由于这可能需要一些时间,因此该数据是异步加载的。 Firebase 不会阻止您的主代码(并使您的应用无响应),而是允许您的主代码继续运行,并在数据可用时调用您的完成处理程序。

这意味着任何需要数据库数据的代码都必须完成处理程序中。

所以在你的情况下是这样的:

let likesDB = Database.database().reference().child("Discounts")
likesDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String , String>
let businessID = Int(snapshotValue["BusinessID"]!)
let businessName = snapshotValue["businessName"]!
let DateNumber = Int(snapshotValue["DateNumber"]!)
let theDeal = snapshotValue["theDeal"]!
let phoneNumber = snapshotValue["PhoneNumberText"]!
let imageName = snapshotValue["ImageName"]!
let dateText = snapshotValue["DateText"]!
let phoneNumberInteger = Int(snapshotValue["phoneNumberInteger"]!)
let companyLogo = snapshotValue["companyLogo"]!
let r1 = Int(snapshotValue["r1"]!)
let r2 = Int(snapshotValue["r2"]!)
let r3 = Int(snapshotValue["r3"]!)
let classification = snapshotValue["classification"]!
let allTheLikes = likeclass()
allTheLikes.discountID = businessID!
allTheLikes.businessName = businessName
allTheLikes.dateApplied = DateNumber!
allTheLikes.theDeal = theDeal
allTheLikes.phoneNumber = phoneNumber
allTheLikes.imageName = imageName
allTheLikes.dateText = dateText
allTheLikes.numberNumber = phoneNumberInteger!
allTheLikes.companylogo = companyLogo
allTheLikes.r1 = r1!
allTheLikes.r2 = r2!
allTheLikes.r3 = r3!
allTheLikes.classification = classification
self.theLikeArray.append(allTheLikes)

print(theLikeArray)
}

请注意,这是一个非常常见的混淆来源,因此我强烈建议您阅读有关该主题的其他一些问题:

关于ios - 为什么我从数据库中提取信息的函数不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58053481/

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