gpt4 book ai didi

swift - 在数据显示之前等待 1-2 秒从 Firebase 获取数据 (IOS)

转载 作者:行者123 更新时间:2023-11-30 10:37:58 25 4
gpt4 key购买 nike

我创建了包含用户信息页面的应用,并且正在使用 Firebase 实时数据库来检索数据。我检索了所有数据,但必须等待 1 - 2 秒才能显示信息。

我应该做什么来解决这个问题?

这就是我从 firebase 读取数据的方式

var ref: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()

getUserDetails()
}

func getUserDetails () {
ref = Database.database().reference().child("Users/Sellers/\(userID)/user_info/")
ref.observeSingleEvent(of: .value, with: { (snapshot) in

let value = snapshot.value as? NSDictionary

self.userIDText.text = value?["userID"] as? String ?? ""
self.userIDText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.changeNameText.text = value?["name"] as? String ?? ""
self.changeNameText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.addressText.text = value?["address"] as? String ?? ""
self.addressText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.emailText.text = value?["email"] as? String ?? ""
self.emailText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.phoneNumberText.text = value?["phone_number"] as? String ?? ""
self.phoneNumberText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

}) { (error) in
print(error.localizedDescription)
}
}

最佳答案

在类之外创建全局变量。按如下方式更改 ViewController。注意:我没有看到你在哪里定义了 ref。

var userDic: NSDictionary?

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

self.userIDText.text = userDic?["userID"] as? String ?? ""
self.userIDText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.changeNameText.text = userDic?["name"] as? String ?? ""
self.changeNameText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.addressText.text = userDic?["address"] as? String ?? ""
self.addressText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.emailText.text = userDic?["email"] as? String ?? ""
self.emailText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)

self.phoneNumberText.text = userDic?["phone_number"] as? String ?? ""
self.phoneNumberText.addTarget(self, action: #selector(self.textFieldDidChange(_:)), for: .editingChanged)
}

}

在 AppDelegate 中:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func getUserDetails () {
ref = Database.database().reference().child("Users/Sellers/\(userID)/user_info/")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
userDic = snapshot.value as? NSDictionary
}) { (error) in
print(error.localizedDescription)
}
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
getUserDetails()
return true // Could test if userDic != nil
}

关于swift - 在数据显示之前等待 1-2 秒从 Firebase 获取数据 (IOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57505863/

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