无限期地保持包含NSTimer的NSThread吗? (苹果手机) [英] Keep an NSThread containing an NSTimer around indefinitely? (iPhone)

查看:57
本文介绍了无限期地保持包含NSTimer的NSThread吗? (苹果手机)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一些Web服务数据,需要每3分钟更新一次. 我已经尝试了几种方法,但是上周在这里得到了很好的建议,我不应该每3分钟构建一个新线程,然后再尝试重新分配并同步所有不同的部分,从而避免出现内存错误.相反,我应该有一个始终在运行的工作线程",但也只有在我询问时(每3分钟)才进行实际工作.

I have some web service data in my app that needs to be updated every 3 minutes. I had tried out a few approaches but got a really good piece of advise in here last week, I should not build a new thread every 3 minutes and then subsequently try and dealloc and synchronize all the different parts so that I avoided memory bug. Instead I should have a "worker thread" that was always running, but only did actual work when I asked it too (every 3 minutes).

当我的小型POC现在可以工作时,我在applicationDidFinishLaunching中产生了一个新线程 方法.我这样做是这样的:

As my small POC works now, I spawn a new thread in the applicationDidFinishLaunching method. I do this like so:

[NSThread detachNewThreadSelector:@selector(updateModel) toTarget:self withObject:nil];

- (void) updateModel {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    BackgroundUpdate *update = [[BackgroundUpdate alloc] initWithTimerInterval:180];
    [update release];
    [pool release];
}

好的,这会以更新间隔(以秒为单位)启动"BackgroundUpdate"对象.在更新程序内部,现在就像这样:

Ok, this inits the "BackgroundUpdate" object with the update interval in seconds. Inside the updater it is simply like this for now:

@implementation BackgroundUpdate

- (id) initWithTimerInterval:(NSInteger) secondsBetweenUpdates {

    if(self = [super init]) {

        [NSTimer scheduledTimerWithTimeInterval:secondsBetweenUpdates 
                                        target:self 
                                        selector:@selector(testIfUpdateNeeded) 
                                        userInfo:nil 
                                        repeats:YES];
    }

    return self;
}

- (void) testIfUpdateNeeded {

    NSLog(@"Im contemplating an update...");

}

我以前从未使用过这样的线程.我一直以来都是设置autoReleasePool,做好工作,耗尽autoReleasePool,然后再见".

I have never used threads like this before. I has always been "setup autoReleasePool, do work, get your autoReleasePool drained, and goodbye".

我的问题是initWithTimerInterval一旦运行,NSThread就完成了,因此它返回到updateModel方法并耗尽了其池.我想这与NSTimer具有自己的线程/运行循环有关吗?我希望线程保持每3分钟运行一次testIfUpdateNeeded方法.

My problem is that as soon as the initWithTimerInterval has run, the NSThread is done, it therefore returns to the updateModel method and has its pool drained. I guess it has to do with the NSTimer having it's own thread/runloop? I would like for the thread to keep having the testIfUpdateNeeded method run every 3 minutes.

那我如何在应用程序的整个过程中保持该NSThread的生命力?

感谢您提供的任何帮助/建议:)

Thank You for any help/advice given:)

推荐答案

您已经关闭.现在您需要做的就是启动运行循环运行,这样线程就不会退出并且计时器会运行.调用initWithTimerInterval:之后,只需调用

You're close. All you need to do now is start the run loop running so the thread doesn't exit and the timer runs. After your call to initWithTimerInterval:, just call

[[NSRunLoop currentRunLoop] run];

该线程将无限期地运行其运行循环,您的计时器将正常工作.

The thread will run its run loop indefinitely and your timer will work.

这篇关于无限期地保持包含NSTimer的NSThread吗? (苹果手机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆