gpt4 book ai didi

ios - 返回 1 个屏幕时,应用程序在加载 Firebase 数据之前崩溃

转载 作者:行者123 更新时间:2023-11-29 05:52:22 26 4
gpt4 key购买 nike

在实际加载数据之前检索数据并离开 View Controller 时,我的 Firebase 功能遇到了一个小问题。我意识到,如果我在实际数据加载到 VC 之前按下 Controller 中的后退按钮,我的控制台就会崩溃,这是 *** 由于未捕获的异常“InvalidPathValidation”而终止应用程序,原因:“(child:) 必须是非空字符串且不包含“.” '#' '$' '[' 或 ']''。仅当我尝试退出得太快,以防用户在加载数据之前想要去其他地方时,才会发生这种情况,否则每个功能都可以正常工作。

我已经在 viewDidDissapear 中删除了两个 Firebase 函数的观察者,这会有所帮助,因为您不想让观察者保持打开状态,但这并不能解决我的崩溃问题。我还有一个进度平视显示器,一旦加载数据就会显示并关闭,但没有成功。例如,下面是我的 Firebase 函数之一,用于检索数据。

func retrieveMessages() {
let ref = Database.database().reference().child("locations").child(titleString)
ref.observe(.childAdded) { (snapshot) in
if snapshot.exists() {
let snapshotValue = snapshot.value as! Dictionary<String,String>
let text = snapshotValue["message"]!
let sender = snapshotValue["username"]!
let img = snapshotValue["userImage"]!
let upvote = snapshotValue["upvote"]!
let timestamp = snapshotValue["timestamp"]!
let message = ChatMessage()
message.msgTimestamp = timestamp
message.messageBody = text
message.sender = sender
message.chatImageURL = img
let upvoteInt = Int(upvote)
message.upvoteString = upvoteInt!
self.chatMessageArray.append(message)
self.chatTable.reloadData()
self.chatRoomScrollToBottom(animated: true)
}
}
}

我的下一个假设是禁用后退按钮,直到数据完全加载?这可以解决这个问题,但我想知道如果我立即按下后退按钮,是否有办法停止此功能或我触发的任何 FB 功能?感谢任何帮助。

最佳答案

也许您可以尝试运行以下命令,并对输出进行注释:

import UIKit
import Firebase

class FirebaseTableViewController: UITableViewController {

lazy var ref: DatabaseReference = Database.database().reference()
var chatMessageArray: Array<ChatMessage> = []
var messageRef: DatabaseReference!
var titleString = "" //TODO: Update this
var chatTable: UITableView = UITableView() //TODO: Update this



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}

override func viewDidLoad() {
super.viewDidLoad()
print(titleString) // TODO: Check that it dose not contain: '.' '#' '$' '[' or ']''
messageRef = ref.child("locations").child(titleString)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
chatMessageArray.removeAll()
messageRef.observe(.childAdded) { (snapshot) in
self.createMessage(snapshot:snapshot)
}
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

// removeAllObservers: Removes all observers at the current reference, but does not remove any observers at child references
messageRef.removeAllObservers()
}

func chatRoomScrollToBottom(animated: Bool) {

}

func createMessage(snapshot: DataSnapshot) {
guard let snapshotValue = snapshot.value as? Dictionary<String,String> else {
print("Error with snapshotValue")
return
}
guard let text = snapshotValue["message"],
let sender = snapshotValue["username"],
let img = snapshotValue["userImage"],
let upvote = snapshotValue["upvote"],
let timestamp = snapshotValue["timestamp"] else {
print("Error with snapshotValue[''] values")
return
}
let message = ChatMessage()
message.msgTimestamp = timestamp
message.messageBody = text
message.sender = sender
message.chatImageURL = img
guard let upvoteInt = Int(upvote) else {
print ("Error with pvoteInt")
return
}
message.upvoteString = String(upvoteInt)
self.chatMessageArray.append(message)
self.chatTable.reloadData()
self.chatRoomScrollToBottom(animated: true)
}

}

希望对您有所帮助!

关于ios - 返回 1 个屏幕时,应用程序在加载 Firebase 数据之前崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546905/

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