如何停止viewWillAppear完成直到函数完成

时间:2016-07-11 15:59:01

标签: ios swift xcode viewwillappear

我对IOS应用程序开发很新。

我试图阻止viewWillAppear完成,直到我的功能完成工作。我该怎么做?

这里是viewWillAppear:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    checkFacts()

    if reset != 0 {
        print("removing all bird facts")
        birdFacts.removeAll()
    }
}

func checkFacts() {
    let date = getDate()
    var x: Bool = true
    var ind: Int = 0
    print("count is ", birdFacts.count)
    while ind < birdFacts.count {
        print("accessing each bird fact in checkFacts")
        let imageAsset: CKAsset = birdFacts[ind].valueForKey("birdPicture") as! CKAsset
        let image = UIImage(contentsOfFile: imageAsset.fileURL.path!)
        print(image)
        if image == nil {
            if (birdFacts[ind].valueForKey("sortingDate") != nil){
                print("replacing fact")
                print("accessing the sortingDate of current fact in checkFacts")
                let sdate = birdFacts[ind].valueForKey("sortingDate") as! NSNumber
                replaceFact(sdate, index: ind)
            }
            /*else {
                birdFacts.removeAll()
                print("removing all bird facts")
            }*/

        }
        ind = ind + 1
        print(ind)
    }
    self.saveFacts()
    let y = checkRepeatingFacts()
    if y {
        print("removing all facts")
        birdFacts.removeAll()
        //allprevFacts(date, olddate: 0)
    }
}

checkFacts引用了其他两个函数,但我不确定它们是否与此相关(但是如果它们存在,我会添加它们并且我错了)

2 个答案:

答案 0 :(得分:2)

您为什么不尝试使用关闭,而不是尝试更改或停止应用程序的实际生命周期?

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(true)

    checkFacts(){ Void in
        if self.reset != 0 {
            print("removing all bird facts")
            birdFacts.removeAll()
        }
    }
}

func checkFacts(block: (()->Void)? = nil) {
    let date = getDate()
    var x: Bool = true
    var ind: Int = 0
    print("count is ", birdFacts.count)
    while ind < birdFacts.count {
        print("accessing each bird fact in checkFacts")
        let imageAsset: CKAsset = birdFacts[ind].valueForKey("birdPicture") as! CKAsset
        let image = UIImage(contentsOfFile: imageAsset.fileURL.path!)
        print(image)
        if image == nil {
            if (birdFacts[ind].valueForKey("sortingDate") != nil){
                print("replacing fact")
                print("accessing the sortingDate of current fact in checkFacts")
                let sdate = birdFacts[ind].valueForKey("sortingDate") as! NSNumber
                replaceFact(sdate, index: ind)
            }
            /*else {
                birdFacts.removeAll()
                print("removing all bird facts")
            }*/

        }
        ind = ind + 1
        print(ind)
    }
    self.saveFacts()
    let y = checkRepeatingFacts()
    if y {
        print("removing all facts")
        birdFacts.removeAll()
        //allprevFacts(date, olddate: 0)
    }

    // CALL CODE IN CLOSURE LAST //
    if let block = block {
        block()
    }
}

根据Apple Documentation

  

闭包是一个独立的功能块,可以在代码中传递和使用。

     

闭包可以从定义它们的上下文中捕获和存储对任何常量和变量的引用。

因此,通过将checkFacts()定义为:func checkFacts(block: (()->Void)? = nil){...},我们可以选择传入要在checkFacts()函数中执行的代码块。

语法block: (()->Void)? = nil意味着我们可以接收将返回void的代码块,但如果没有传入任何内容,block将只是nil。这允许我们在使用或不使用闭包的情况下调用函数。

使用:

if let block = block {
    block()
}

我们可以安全地致电block()。如果blocknil的形式返回,我们会将其传递过来并假装没有任何反应。如果block 不是 nil,我们可以执行其中包含的代码,然后继续前进。

我们可以将闭包代码传递给checkFacts()的一种方法是通过尾随闭包。尾随闭包看起来像这样:

checkFacts(){ Void in
    if self.reset != 0 {
        print("removing all bird facts")
        birdFacts.removeAll()
    }
}

修改:添加了语法说明。

答案 1 :(得分:1)

因此,基于注释,checkFacts正在调用异步iCloud操作,如果它们未完成,将导致视图无法管理的空数据。

举起viewWillAppear不是管理它的方法 - 这只会导致用户界面延迟,从而刺激用户。

首先,您的视图应该能够管理空数据而不会崩溃。即使你解决了这个问题,也有可能在其他情况下数据变坏并且用户讨厌崩溃。所以我建议你解决这个问题。

修复原始问题:允许使用未经检查的数据加载视图。然后触发checkData进程,并在完成后发布NSNotification。让您的视图监视该通知并在其发生时重绘其内容。 (可选)如果您不希望用户与未经检查的数据进行交互:禁用适当的控件并显示活动指示符,直到通知发生。

相关问题