开发者

iphone, multithread

开发者 https://www.devze.com 2023-02-01 06:39 出处:网络
my application launch 2 threads who parse a XML file and in the main thread i put a condition: while(parse == YES)

my application launch 2 threads who parse a XML file and in the main thread i put a condition: while(parse == YES) { nslog("do nothing"); } [tableview reloadata];

but when i read the array variable in the function "cellforpathatindex" i got an error like : "i try to read an empty array" but when i debut or make sleep the main thread it is working fine. what to do to get the array variable directly there when the pa开发者_如何学Crsing is finis


There are two common patterns that you can use in this situation, and both work by having the worker threads tell the main thread when their work (XML parsing) has finished:

  • when the worker threads have finished their work, they post notifications via NSNotificationCenter
  • define a delegate protocol and have a class in the main thread implement it. Pass an instance of that class to the worker threads — it'll be a delegate. When the worker threads have finished their work, they send a message to the delegate (e.g. -parseFileDidFinish:)


@gjd nice ans :) 2nd option is the one which @user482611 should use. Don't reload the table on the main thread untill and unless the parsing is finished.

0

精彩评论

暂无评论...
验证码 换一张
取 消