我不明白为什么databaseRef withBlock永远不会执行。
我已经用 Swift 制作了一个可以工作的实时聊天应用程序,一切都按预期工作,所以我对此并不陌生......
用户使用 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:"
最佳答案
您可能会收到错误。
// 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?
}
关于ios - Firebase:obvserveEventType和observeSingleEventOfType withBlock中的关闭:不执行(使用程序集),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39949925/