Ios 在函数完成之前,如何停止查看将从完成开始出现
我对IOS应用程序开发相当陌生 我正在尝试停止ViewWillDisplay,直到我的函数完成工作。我该怎么做 将出现以下视图:Ios 在函数完成之前,如何停止查看将从完成开始出现,ios,swift,xcode,viewwillappear,Ios,Swift,Xcode,Viewwillappear,我对IOS应用程序开发相当陌生 我正在尝试停止ViewWillDisplay,直到我的函数完成工作。我该怎么做 将出现以下视图: override func viewWillAppear(animated: Bool) { super.viewWillAppear(true) checkFacts() if reset != 0 { print("removing all bird facts") birdFacts.removeAll
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)
}
}
覆盖函数视图将出现(动画:Bool){
super.viewwillbeen(true)
核对事实()
如果重置!=0{
打印(“删除所有鸟类事实”)
birdFacts.removeAll()
}
}
func checkFacts(){
let date=getDate()
变量x:Bool=true
变量ind:Int=0
打印(“计数为”,birdFacts.count)
而ind
checkFacts引用了其他两个函数,但我不确定它们是否与此处相关(但如果它们是相关的,我会将它们添加进来,如果我弄错了)与其尝试更改或停止应用程序的实际生命周期,不如尝试使用闭包
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()
}
}
我们可以安全地调用block()
。如果block
返回为nil
,我们会跳过它,假装什么都没发生。如果block
不是nil
,我们可以执行其中包含的代码,继续前进
我们可以将闭包代码传递到checkFacts()
中的一种方法是使用尾随闭包。尾随闭包如下所示:
checkFacts(){ Void in
if self.reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}
编辑:添加了语法解释。因此,根据注释,checkFacts调用异步iCloud操作,如果这些操作不完整,将导致视图无法管理的空数据 按住ViewWillAppeal并不是管理此问题的方法-这只会导致用户界面延迟,从而激怒用户 首先,视图应该能够在不崩溃的情况下管理空数据。即使您解决了这个问题,也可能会有其他情况下数据变糟,用户讨厌崩溃。所以我建议你解决这个问题
修复原始问题:允许视图加载未选中的数据。然后触发checkData进程,并在其完成后发布通知。让您的视图监视该通知,并在通知发生时重新绘制其内容。或者,如果您不希望用户与未检查的数据交互:禁用相应的控件并显示活动指示器,直到通知出现。是否需要在ViewWill出现之前完成checkFacts?是的,checkFacts需要首先完成,如图所示,它将在函数ViewWill出现之前完成(除非在checkFacts中调用的那些函数中有一些异步操作)所以我想我遗漏了一些东西-你的意思是你不想在checkFacts完成之前显示视图吗?我遇到的问题是checkFacts正在查找重复的事实,这会导致函数删除所有事实并从云中重新下载一个新集。但是,当tableView运行时,它会尝试查找不存在的事实并崩溃修改代码。你介意向我解释一下语法吗,因为我以前没有使用过块?@Alex我已经用一个解释更新了我的答案,我希望这有助于让一切更容易理解。好的,这现在更有意义了。我遇到的问题是checkFacts发现重复的事实,这会导致函数删除所有事实并重新执行-从云端下载一个新的集合。然而,当tableView运行时,它会试图找到不存在的事实并使代码崩溃。我尝试了你的代码,但这并没有解决问题,有什么具体的事情我应该做吗?如果是这样的话,我想你的问题不在于等待函数完成其代码,而可能更多的是如何处理我们正在处理重复的事实。
checkFacts(){ Void in
if self.reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}