在检索数据并在实际加载数据之前离开视图控制器时,我遇到了关于Firebase功能的小问题。我意识到,如果在将实际数据加载到VC之前按控制器中的后退按钮,则会在控制台*** Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
中崩溃。仅当我尝试退出得太快时,如果用户想在加载数据之前先走到其他地方,否则这种情况就会发生。
我已经在viewDidDissapear中删除了两个Firebase函数的观察者,这很有帮助,因为您不想让观察者保持打开状态,但这不能解决我的崩溃问题。我也有一个进度HUD,一旦加载数据,它就会显示并关闭,但不会成功。例如,以下是我的Firebase函数中的1个,它们正在检索数据。
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功能?感谢任何帮助。
答案 0 :(得分:0)
也许您可以尝试运行以下命令并注释输出:
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)
}
}
希望有帮助!