[英]How to stop viewWillAppear from completing until a function has finished
我是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个函数,但是我不确定它们是否在此处相关(但是如果它们被误认为是错误的,我会添加它们)
与其尝试更改或停止应用程序的实际生命周期,不如不尝试使用闭包 ?
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文档 :
闭包是可以独立传递的功能块,可以在代码中传递和使用。
闭包可以从定义它们的上下文中捕获和存储对任何常量和变量的引用。
因此,通过将checkFacts()
定义为: func checkFacts(block: (()->Void)? = nil){...}
我们可以有选择地传入要在checkFacts()
函数中执行的代码块。
语法block: (()->Void)? = nil
block: (()->Void)? = nil
意味着我们可以接受一个代码块,该代码块将返回void,但是如果未传入任何内容,则代码block
将只是nil
。 这使我们可以在使用或不使用闭包的情况下调用函数。
通过使用:
if let block = block {
block()
}
我们可以安全地调用block()
。 如果block
返回nil
,我们将其跳过并假装什么都没有发生。 如果block
不是 nil
,我们可以执行其中包含的代码,然后继续进行。
我们可以将闭包代码传递到checkFacts()
一种方法是通过尾随闭包。 尾随闭包看起来像这样:
checkFacts(){ Void in
if self.reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}
编辑:添加了语法说明。
因此,基于注释,checkFacts正在调用异步iCloud操作,如果这些操作不完整,将导致您的视图无法管理的空数据。
阻止viewWillAppear并不是管理此问题的方法-只会导致用户界面延迟,这会激怒您的用户。
首先,您的视图应该能够管理空数据而不会崩溃。 即使解决了此问题,也可能在其他情况下数据变坏并且用户讨厌崩溃。 因此,我建议您修复该问题。
要解决原始问题,请执行以下操作:允许视图加载未经检查的数据。 然后触发checkData进程,并在完成时发布NSNotification。 使视图监视该通知,并在发生通知时重绘其内容。 (可选)如果您不希望用户与未经检查的数据进行交互:请禁用适当的控件并显示活动指示器,直到发生通知为止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.