【发布时间】:2017-01-18 20:58:31
【问题描述】:
我正在使用 tabBar 控制器。 TabOne 是 rootVC。在 TabOne 中,我有一个 viewController,它通过 sendButton 向 Firebase 发送信息并以编程方式呈现 TabTwo。
在 TabTwo 中,我有获取信息并显示它的 tableView 控制器。奇怪的是,我运行了两次代码,一切都很好,我按下了发送按钮,出现了 TabTwo,它的表格视图正确地加载了具有正确信息的单元格。现在的问题是由于某种奇怪的原因,它只成功运行了这 2 次,并且从那时起它一直在 dispatch_async 上崩溃。
现在我只是尝试运行模拟器,而不是在 TabOne 中按下 sendButton,而是按下 TabTwo 的选项卡,应用程序在 dispatch_async 上崩溃。我没有尝试向 Firebase 发送任何内容,我只是单击了 TabTwo 的选项卡,并且 dispatch_async 因代码而崩溃:
Thread1: EXC_BAD_INSTRUCTION (code=EXC_1338_INVOP, subcode=0x0)
知道为什么我切换标签时它似乎崩溃了吗? 知道为什么一切正常运行两次然后突然崩溃吗?
代码如下:
模型对象
class CookieModel: NSObject{
var name: String?
}
标签1
class TabOneController: UIViewController{
var dbRef: FIRDatabaseReference!
let userID: String? = (FIRAuth.auth()?.currentUser?.uid)!
let cookieModel = [CookieModel]()
override func viewDidLoad() {
super.viewDidLoad()
self.dbRef = FIRDatabase.database().reference()
}
@IBAction func sendButton(sender: UIButton){
self.sendToFireBase()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = mainStoryboard.instantiateInitialViewController() as! UITabBarController
tabBarController.selectedIndex = 1
self.presentViewController(tabBarController , animated: true, completion: nil)
}
//Code to send data to FirebaseDatabase
func sendToFireBase(){
let uniquePath = NSUUID().UUIDString
let usersIDRef = self.dbRef.child("users").child(self.userID!)
let cookiePath = usersIDRef.child("cookieData").child(self.uniquePath)
let cookie0 = CookieModel()
cookie0.name = "oatmeal"
self.cookieModel.append(cookie0)
let cookie1 = CookieModel()
cookie1.name = "chocolate"
self.cookieModel.append(cookie1)
let cookie2 = CookieModel()
cookie2.name = "coconut"
self.cookieModel.append(cookie2)
var cookieDict = [String:AnyObject]()
cookieDict.updateValue(cookie0.name!, forKey: "cookie0")
cookieDict.updateValue(cookie1.name!, forKey: "cookie1")
cookieDict.updateValue(cookie2.name!, forKey: "cookie2")
cookiePath.updateChildValues(cookieDict, withCompletionBlock: {
(error, user) in
if error != nil{
print((error?.localizedDescription))
return
}
}
}
tab2
class TabTwoController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var tableView: UITableView!
var dbRef: FIRDatabaseReference!
let userID: String? = (FIRAuth.auth()?.currentUser?.uid)!
var cookieModel = [CookieModel]()
override func viewDidLoad() {
super.viewDidLoad()
self.dbRef = FIRDatabase.database().reference()
self.tableView.delegate = self
self.observeFBCookieData()
}
//Code to retrieve data from FirebaseDatabase
func observeFBCookieData(){
let usersIDRef = self.dbRef.child("users").child(self.userID!)
let cookiePath = usersIDRef.child("cookieData")
cookiePath.observeEventType(.ChildAdded, withBlock: {
(snapshot) in
if let dict = snapshot.value as? [String:AnyObject]{
let cookie0 = dict["cookie0"] as? String
let cookie1 = dict["cookie1"] as? String
let cookie2 = dict["cookie2"] as? String
let cookie0 = CookieModel()
cookie0.name = cookie0!
self.cookieModel.append(cookie0)
let cookie1 = CookieModel()
cookie1.name = cookie1!
self.cookieModel.append(cookie1)
let cookie2 = CookieModel()
cookie2.name = cookie2!
self.cookieModel.append(cookie2)
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}, withCancelBlock: nil)
}
//MARK: -TableViewDatasource Methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.cookieModel.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCookieCell
cell.titleLabel.text = self.cookieModel[IndexPath.row].name!
return cell
}
}
【问题讨论】:
-
完整的错误是什么?究竟是哪一行导致了错误?
-
@rmaddy 你好,谢谢。这是错误行: dispatch_async(dispatch_get_main_queue(), {
-
这是错误:Thread1: EXC_BAD_INSTRUCTION (code=EXC_1338_INVOP, subcode=0x0)
标签: ios xcode asynchronous swift2 firebase-realtime-database