ios - Activity Indicator with loop -
i'm working app has loop lot of work. loop generates bunch of numbers, , puts them in uitableview
. want display uiactivityindicatorview
when work going on. added activity indicator screen , centered it. @ first, wasn't getting activity indicator display @ all. realized because activity indicator running on same thread loop, , indicator never update while loop running. did research on how create background thread, , came this.
self.progressindicator.startanimating() dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0), { () -> void in var instance = 0; instance < numbercount!; instance++ { //lots of work done in here. items added //collection used populate table view. //takes around 10 seconds execute. } nsnotificationcenter.defaultcenter().postnotificationname("finishednumbers", object: nil) });
the activity indicator running on main thread, , processing of loop running in background. created notifier calls out function call reloadlist , stop activity indicator once list of numbers has completed.
func donewithcreatenumbers(notification: nsnotification) { self.numberlist.reloaddata() self.progressindicator.stopanimating() }
while work, doesn't work well. there couple of problems.
even though processing of loop done in under 10 seconds, takes longer list populate , activity indicator stop spinning. put breakpoint in donewithcreatenumbers function , checked count of numbers collection , have correct number of items in it. after code reload list , stop activity indicator executes, takes between 30 , 40 seconds list populate , activity indicator stop running.
the list populate , indicator goes away, error message in debug window:
this application modifying autolayout engine background thread, can lead engine corruption , weird crashes. cause exception in future release.
i've tried rearranging of in number of ways, nothing has worked better have now. 1 of ways rearranged put reloaddata , stopanimating right after loop. didn't work better, , still got error listed above.
i'm missing here, not sure what. ideas?
try out:
let activityindicator = uiactivityindicatorview.init(activityindicatorstyle:uiactivityindicatorviewstyle.whitelarge) self.view.addsubview(activityindicator) // switch background thread dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_background, 0)) { () -> void in // animate activity indicator on main thread dispatch_async(dispatch_get_main_queue(), { () -> void in activityindicator.startanimating() }) // table calculation work here // stop animating activity indicator on main thread dispatch_async(dispatch_get_main_queue(), { () -> void in activityindicator.stopanimating() }) }
Comments
Post a Comment