【问题标题】:run methods in background in iphone在iphone的后台运行方法
【发布时间】:2010-10-15 05:38:28
【问题描述】:

我想知道如何在后台运行任何方法...我正在制作从互联网解析 xml 文件的应用程序..文件很大..因此解析它并在表中显示需要 20-25 秒查看...问题是当我按下按钮来解析文件时..按钮保持按下状态直到文件被解析..所以最多 20 秒按钮保持按下状态..对于 thr 用户来说看起来很奇怪..(用户认为就像应用程序挂起..)所以我的想法是显示活动指示器并在后台运行解析方法...当解析结束时停止指示器并在表格视图中显示结果..任何其他简单的方法来实现相同的。 . 这样按钮就不会一直按下...???

【问题讨论】:

    标签: iphone multithreading


    【解决方案1】:

    查看 Apple 的 SeismicXML 示例代码,它使用 NSOperation 和 NSOperationQueue 在后台线程中下载和解析 XML。这样您就可以看到在实际应用程序的上下文中运行的完整实现。

    【讨论】:

      【解决方案2】:

      线程

      1. 创建新线程:

         [NSThread detachNewThreadSelector:@selector(myParse) toTarget:self withObject:nil];
        
      2. 创建新线程调用的方法:

        - (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];
         }
        

      请参阅iphone sdk example 了解更多信息。

      一切顺利。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 2012-12-22
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多