简体   繁体   English

Firebase dispatch_async不断崩溃-swift 2 iOS9

[英]Firebase dispatch_async keeps crashing -swift 2 iOS9

I'm using a tabBar Controller. 我正在使用tabBar控制器。 TabOne is the rootVC. TabOne是rootVC。 In TabOne I have a viewController that is sending info to Firebase via a sendButton and programmatically presenting TabTwo. 在TabOne中,我有一个viewController,它通过sendButton将信息发送到Firebase并以编程方式呈现TabTwo。

In TabTwo I have tableView controller that gets the info and displays it. 在TabTwo中,我有tableView控制器来获取信息并显示它。 Oddly twice I ran the code and everything was fine, I pressed the sendButton, TabTwo gets presented, it's tableview correctly loads the cells with the correct info. 奇怪的是我运行了两次代码,一切都很好,我按下了sendButton,出现了TabTwo,它的tableview正确地加载了具有正确信息的单元格。 The problem is now for some strange reason it only successfully ran those 2 times and it has been crashing on dispatch_async ever since. 现在的问题是由于某种奇怪的原因,它仅成功运行了两次,并且此后一直在dispatch_async上崩溃。

Now I just tried to run the simulator and instead of pressing the sendButton in TabOne I pressed the tab for TabTwo and the app crashes on dispatch_async. 现在,我只是尝试运行模拟器,而不是按TabOne中的sendButton,而是按TabTwo的选项卡,应用程序在dispatch_async上崩溃。 I didn't attempt to send anything to Firebase, I just clicked the tab for TabTwo and dispatch_async crashed with code: 我没有尝试向Firebase发送任何东西,我只是单击TabTwo的选项卡,然后dispatch_async崩溃并显示代码:

Thread1: EXC_BAD_INSTRUCTION (code=EXC_1338_INVOP, subcode=0x0)

Any idea why it seems to crash when I switch tabs? 知道为什么切换选项卡时似乎会崩溃吗? Any idea why everything ran fine twice then all of a sudden crash? 知道为什么一切运行正常两次然后突然崩溃吗?

Here is the code: 这是代码:

ModelObject 模型对象

class CookieModel: NSObject{
     var name: String?
}

tab1 tab1

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 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

    }

 } 

设置表视图的数据源后,尝试在其viewDidLoad中设置它的数据源。

Self.tableView.datasource = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM