makeFateForSegue等待Realm数据库写入完成后

时间:2015-07-29 16:40:44

标签: swift cocoa realm

在我的程序中,当按下按钮时,我正在向数据库添加信息,包括创建发票编号,然后调用segue到新的视图控制器。当调用新的视图控制器时,我想传递该发票号码。一切正常,我可以传递样本数据没问题。然而,似乎"覆盖func prepareForSegue(segue:NSStoryboardSegue,sender:AnyObject!){}"在我的按钮之前调用(在视图控制器初始化时?),所以我传递一个空白值。如何让我的prepareForSegue等到按下按钮之后?这是我目前的代码。

@IBAction func createInvoice(sender: AnyObject) {
     let realm = Realm()
    let invoicepull = Invoice()
    let invoicecount = realm.objects(Invoice)
    let invoicenraw = invoicecount.count
    let a = 100
    let invoicenumber = a + invoicenraw
    var invoicefile = Invoice()
    invoicefile.inumber = invoicenumber
    invoicefile.cnumber = clientcombo.stringValue
    invoicefile.cost = owed.doubleValue
    invoicefile.paid = paid.doubleValue
    invoicefile.sevicecode = service.stringValue
    invoicefile.dateofservice = NSDate()
    // Save your object
    realm.beginWrite()
    realm.add(invoicefile)
    realm.commitWrite()
    //Sent notification
    performSegueWithIdentifier("cinvoiceseuge", sender: nil)
 println("Inside Action")
    println(invoicenumber)
    dismissViewController(self)
}

override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == "cinvoiceseuge") {
        //Checking identifier is crucial as there might be multiple
        // segues attached to same view
        var detailVC = segue.destinationController as! invociegenerator;
        detailVC.toPass = invoicenumber
        println("Inside Sugue")
        println(invoicenumber)
    }
} 

更新:我相信这是Realm数据库的一个问题,导致它出现意外行为。如果我删除所有领域代码,程序按预期工作,我可以传递一个静态虚拟值。

1 个答案:

答案 0 :(得分:0)

createInvoice()中的invoicenumber是一个局部变量,prepareForSegue()中的invoicenumber似乎是一个实例变量。这是你的期望吗?