【问题标题】:iphone app background NSTimer (Poll data at regular interval)iphone app后台NSTimer(定期轮询数据)
【发布时间】:2012-01-08 03:23:47
【问题描述】:

我需要定期从服务器后台获取数据, Nstimer 似乎无法在后台运行。

我是否需要编辑 Void 或音频的 plist 以使 NStimer 在后台工作? 谁能解释一下我们示例代码的过程。

问候, 阿维纳什

【问题讨论】:

  • "Nstimer 似乎无法在后台运行。"代码?
  • 我认为他想说的是,当他的应用程序处于后台时,他的代码不会运行,因为应用程序已暂停。
  • 是的,处于挂起模式,这样我就可以继续从服务器轮询数据

标签: iphone background nstimer


【解决方案1】:

看看这个方法:

[NSThread detachNewThreadSelector: toTarget: withObject:]

示例用法:

 -(void) pollBackground
 {
     @autoreleasepool {

          // every five seconds, update list
          NSTimeInterval interval = 5; 

          while (poll)
          {
              // update from server

              [NSThread sleepForTimeInterval:interval];
          } 

     }
 }

 -(void) viewDidLoad {
     [NSThread detachNewThreadSelector:@selector(pollBackground) toTarget:self withObject:nil];
 }

这应该在后台继续运行。

【讨论】:

  • 我需要在应用程序处于后台模式时访问数据,即当用户单击“主页按钮”时
猜你喜欢
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多