我无法弄清楚为什么databaseRef withBlock永远不会执行。
我已经在Swift& amp;中创建了一个实时聊天应用程序一切都按预期工作,所以我并不是全新的......
用户使用Facebook身份验证登录LoginViewController并转发到此主视图。推文存储在Firebase数据库中。
当我设置断点并逐步执行此操作时,它跳转到第二个错误闭包,重复两次错误关闭&然后进入装配说明。 (在帖子底部)
我已经尝试了各种方法来调用observeEventType和observeSingleEventOfType(这是我想要做的),结果在这里总是一样的。
我已将所有对象打印到控制台,以确保用户已登录并使用FirebaseAuth。
import UIKit
import FirebaseAuth
import FirebaseDatabase
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
var loggedInUser = FIRAuth.auth()?.currentUser
var loggedInUserDetails = AnyObject?()
@IBOutlet weak var homeTableView: UITableView!
var tweets = [AnyObject?]()
override func viewDidLoad() {
super.viewDidLoad()
let databaseRef = FIRDatabase.database().referenceWithPath("user_profile/\(self.loggedInUser!.uid)")
print(self.loggedInUser!uid) // Prints uid
// get the logged in user -- POINT OF FAILURE
databaseRef.observeSingleEventOfType(.Value, withBlock: {(snapshot) in
//store the logged in user's details
self.loggedInUserDetails = snapshot
//get all the tweets by the user
databaseRef.child("tweets/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: {(snapshot:FIRDataSnapshot) in
self.tweets.append(snapshot)
self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
}) {(error) in
print(error.localizedDescription)
}
}) {(error) in
print("Error 1") // never prints
print(error.localizedDescription) //never prints
}
}
我还注意到,当使用xcode逐步执行汇编指令时,它会进入取消回调调用。
这是它开始的地方(第1行)
0x10f5b3db8 <+36>: movq %r12, -0x30(%rbp)
0x10f5b3dbc <+40>: movq 0x1a7a8d(%rip), %rax ; (void *)0x000000010f762e70: FIRDatabaseReference
0x10f5b3dc3 <+47>: movq %rax, -0x28(%rbp)
0x10f5b3dc7 <+51>: movq 0x19f39a(%rip), %rsi ; "observeSingleEventOfType:withBlock:withCancelBlock:"
0x10f5b3dce <+58>: leaq -0x30(%rbp), %rdi
然后在几次跳转后执行该指令:
0x10f5b874e <+291>: movq 0x19ef03(%rip), %rsi ; "observeChildEventWithHandle:withCallbacks:cancelCallback:"
答案 0 :(得分:0)
您可能会收到错误。
// get the logged in user -- POINT OF FAILURE
databaseRef.observeSingleEventOfType(.Value, withBlock: {(snapshot) in
//store the logged in user's details
self.loggedInUserDetails = snapshot
//get all the tweets by the user
databaseRef.child("tweets/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: {(snapshot:FIRDataSnapshot) in
self.tweets.append(snapshot)
self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
}) {(error) in
print(error.localizedDescription)
}
}) {(error) in
print(error.localizedDescription) // maybe permission denied?
}
答案 1 :(得分:0)
毕竟不是Xcode问题......问题是tableView(numberOfRowsInSection)返回了错误的行数&amp;这搞乱了一切。
我了解到Firebase并不总是按照您期望的顺序执行操作。
一旦我发现了问题,我就进入调试器并进入viewDidLoad,点击observeSingleEvent,然后跳到关闭的末尾....就像我认为是问题一样。但后来它转到我的tableView() - &gt; UITableView函数并开始执行,然后返回到我们正在尝试获取数据的observeSingleEvent。
几乎看起来Firebase闭包很懒散。
如果我发现有关此主题的更多内容,我将解决此问题。
干杯!