Iphone,我怎样才能避免看门狗杀死我的应用程序需要很长时间才能启动?

Lol*_*Run 2 iphone watchdog

什么是通知看门狗的正确方法,我不是在无限循环,我仍在加载,所以不要杀我的应用程序?

我在我的崩溃日志中收到异常类型:00000020异常代码:0x8badf00d,仅当从iphone独立运行应用程序时才从xcode

代码花费的时间是:

- (void)viewDidLoad {
    [super viewDidLoad];
 Reachability* reachability = [Reachability sharedReachability];
 [reachability setHostName:@"www.apps2you.com"];    // set your host name here
 NetworkStatus remoteHostStatus = [reachability remoteHostStatus];

 if (remoteHostStatus == ReachableViaWiFiNetwork||remoteHostStatus == ReachableViaCarrierDataNetwork )
 {
  //getting the xml file and then getting the ad images online to display as splah ads.
 }
 else {
//or launch the main interface if there's no connectivity.
  [self DisplayTabbar];
 }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dar*_*ust 5

如果你有一些需要很长时间的初始化,那么最好在一个新线程(via performSelectorInBackground:withObject:)中运行它.然后,您的UI将以某种"锁定"状态启动.创建一个"解锁"UI的方法.作为后台方法的最后一个操作,运行该解锁方法performSelectorOnMainThread:withObject:waitUntilDone:.

重要的是不要阻止主线程,以便运行循环可以响应iOS事件.这就是为什么你应该避免sleep或其他阻止的东西.不过,可以阻止另一个线程.此外,适应基于事件的编程方法有很大帮助.

更新:

如今,最好用dispatch_async而不是performSelectorInBackground:withObject:.您仍然应该NSAutoreleasePooldispatch_async块内创建一个新的以避免内存泄漏.

  • 没错.您需要在后台方法中设置自动释放池.请参阅[this](http://stackoverflow.com/questions/929485/why-is-there-no-autorelease-pool-when-i-do-performselectorinbackground)或[this](http:// stackoverflow. com/questions/2441856/iphone-sdk -leaking-memory-with-performselectorinbackground)问题. (2认同)