iphone - How to perform network task in background thread while updating UITableView? -
just trying figure out best way design such functionality? send asynchronous nsurlconnection hits server sends bunch of data. once data have perform work on pretty expensive , rather in bg thread prevent ui locking. lastly need have uitable update dynamically response server received , processed.
my question how go doing work in background thread as data arrives table doesnt wait until data has loaded before being updated??
this pseudo code have far. in viewcontroller have 2 bool flags newdatareceived , datafinisheddownloading. have 2 variables, string contains current data , nsarray kept results of processing data. then,
- in didreceiveresponse: spawn new thread calling performselectorinbackground: processing method selector.
- in method have loop first check newdatareceived see if new data has arrived , if work on it.
- once finished processing set nsarray results , call method updates table datasource , reloads table using performselectoronmainthread:
- lastly check datafinishedloading flag see if there more data process
- if there still data , start on again, otherwise cleanup thread , exit
also newdatareceived flag set in didreceivedata: actual data received. in didfinishloading set datafinishedloading flag signal data has been loaded.
i plan on using nslock's in #2-4 when checking status flags getting , setting received data string , results nsarray.
im sure there number of ways same thing seem way go it?
thx
you take @ nsoperation , nsoperationqueue. nsoperation perfect alternative doing heavy calculations , operations in background. if need continuously updates tableview implement protocol in operation handle callbacks tableview.
Comments
Post a Comment