iOS swift while循环冻结我的应用程序

时间:2015-07-22 09:52:05

标签: ios swift

我正在开发一个应用程序,用户可以选择住宿并从日期选择器中选择一个时间,这样用户就可以知道巴士将在什么时间到达他的住所,在模拟器和iPad上我有但是我在iPhone真实设备上遇到了这个问题,这是代码:

mTimeString = "5:07 AM"

如果没有找到它将在数组中搜索,减去秒,直到它匹配数组中的结果。虽然循环冻结我的应用程序,我试图通过以下方式包围它:

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT ,0)
dispatch_async(queue){}

它不再冻结,但我根据数组

没有得到结果
if(defaults.stringForKey(accommodationChoiceKey)?.toInt() == 40) {

    var  array = ["12:05 AM","12:25 AM","12:45 AM","1:05 AM","1:25 AM","1:45 AM","2:05 AM","2:25 AM","2:45 AM","3:05 AM","3:25 AM","3:45 AM","4:05 AM","4:25 AM","4:45 AM","5:05 AM","5:25 AM","5:45 AM","6:05 AM","6:25 AM","6:45 AM","7:05 AM","7:25 AM","7:45 AM","8:05 AM","8:25 AM","8:45 AM","9:05 AM","9:25 AM","9:45 AM","10:05 AM","10:25 AM","10:45 AM","11:05 AM","11:25 AM","11:45 AM","12:05 PM","12:25 PM","12:45 PM","1:05 PM","1:25 PM","1:45 PM","2:05 PM","2:25 PM","2:45 PM","3:05 PM","3:25 PM","3:45 PM","4:05 PM","4:25 PM","4:45 PM","5:05 PM","5:25 PM","5:45 PM","6:05 PM","6:25 PM","6:45 PM","7:05 PM","7:25 PM","7:45 PM","8:05 PM","8:25 PM","8:45 PM","9:05 PM","9:25 PM","9:45 PM","10:05 PM","10:25 PM","10:45 PM","11:05 PM","11:25 PM","11:45 PM"]
    while (find(array, mTimeString) == nil) {
        choiceToSeconds--
        var newmTime = x.dateByAddingTimeInterval(Double(choiceToSeconds))
        var mTimeString = dateFormatter.stringFromDate(newmTime)
        if (find(array, mTimeString) != nil) {
            transportationLabel.text = mTimeString
            break
        }
        getReady(newmTime)
    }
}

2 个答案:

答案 0 :(得分:2)

你正在计算背景线程,这是好的,但你不应该从后台线程(transportationLabel.text = mTimeString)更新UI元素,这应该从主线程完成,例如

    let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
    dispatch_async(dispatch_get_global_queue(priority, 0)) {

         if(defaults.stringForKey(accommodationChoiceKey)?.toInt() == 40) {

                var  array = ["12:05 AM","12:25 AM","12:45 AM","1:05 AM","1:25 AM","1:45 AM","2:05 AM","2:25 AM","2:45 AM","3:05 AM","3:25 AM","3:45 AM","4:05 AM","4:25 AM","4:45 AM","5:05 AM","5:25 AM","5:45 AM","6:05 AM","6:25 AM","6:45 AM","7:05 AM","7:25 AM","7:45 AM","8:05 AM","8:25 AM","8:45 AM","9:05 AM","9:25 AM","9:45 AM","10:05 AM","10:25 AM","10:45 AM","11:05 AM","11:25 AM","11:45 AM","12:05 PM","12:25 PM","12:45 PM","1:05 PM","1:25 PM","1:45 PM","2:05 PM","2:25 PM","2:45 PM","3:05 PM","3:25 PM","3:45 PM","4:05 PM","4:25 PM","4:45 PM","5:05 PM","5:25 PM","5:45 PM","6:05 PM","6:25 PM","6:45 PM","7:05 PM","7:25 PM","7:45 PM","8:05 PM","8:25 PM","8:45 PM","9:05 PM","9:25 PM","9:45 PM","10:05 PM","10:25 PM","10:45 PM","11:05 PM","11:25 PM","11:45 PM"]
                while (find(array, mTimeString) == nil) {
                    choiceToSeconds--
                    var newmTime = x.dateByAddingTimeInterval(Double(choiceToSeconds))
                    var mTimeString = dateFormatter.stringFromDate(newmTime)
                    if (find(array, mTimeString) != nil) {

                        //  This should be done on Main Thread
                        dispatch_async(dispatch_get_main_queue()) {
                            transportationLabel.text = mTimeString
                        }      


                        break
                    }
                    getReady(newmTime)
                }
            }
    }

答案 1 :(得分:0)

尝试替换

transportationLabel.text = mTimeString

通过

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
     transportationLabel.text = mTimeString
}];

必须从主线程执行UI操作

编辑:

在斯威夫特:

NSOperationQueue.mainQueue().addOperationWithBlock({
    transportationLabel.text = mTimeString
})