开发者

run methods in background in iphone

开发者 https://www.devze.com 2023-01-19 22:20 出处:网络
i want to know how to run any method in background...i am making app that parse xml file from internet..the file is large..so it takes 20-25 secon开发者_运维知识库ds to parse it and show it in table v

i want to know how to run any method in background...i am making app that parse xml file from internet..the file is large..so it takes 20-25 secon开发者_运维知识库ds to parse it and show it in table view...the problem is that when i press the button to parse the file..the button remains pressed until the file is parsed..so upto 20 sec button remains pressed..it looks odd for thr user ..(user thinks like the app hangs..)so my idea is to show the activity indicator and run the parsing method in background...when parsing ends stop the indicator and show the results in table view..any other easy way to implement the same..so that the button does not remain pressed...???


Check out Apple's SeismicXML sample code which uses NSOperation and NSOperationQueue to download and parse XML in a background thread. That way you can see a full implementation working in the context of an actual application.


Threading

  1. Create the new thread:

     [NSThread detachNewThreadSelector:@selector(myParse) toTarget:self withObject:nil];
    
  2. Create the method that is called by the new thread:

    - (void)myParse {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     *** code that should be run in the new thread goes here *** 
       //sample
       NSURL *url;
    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
      resourcePath = [resourcePath stringByAppendingPathComponent:@"sample.xml"];
    url = [[NSURL fileURLWithPath:resourcePath] retain];
    [self parsingTheXMLAtUrl:url];
       [pool release];
     }
    

see iphone sdk example for more information.

All the best.

0

精彩评论

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