无限期地保留包含 NSTimer 的 NSThread? (iPhone)

发布于 2024-08-22 19:30:13 字数 1588 浏览 3 评论 0原文

我的应用程序中有一些 Web 服务数据需要每 3 分钟更新一次。 我已经尝试了几种方法,但上周在这里得到了一个非常好的建议,我不应该每 3 分钟构建一个新线程,然后尝试释放和同步所有不同的部分,这样我就可以避免内存错误。相反,我应该有一个始终运行的“工作线程”,但只有在我也要求它时才执行实际工作(每 3 分钟一次)。

由于我的小型 POC 现在可以工作,我在 applicationDidFinishLaunching 中生成一个新线程 方法。我这样做是这样的:

[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”对象,更新间隔以秒为单位。在更新程序内部,现在就像这样:

@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,然后再见”。

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

那么我如何在我的应用程序的整个运行期间保持这个 NSThread 处于活动状态?

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

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).

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

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...");

}

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

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.

So how will I keep this NSThread alive for the entire duration of my app?

Thank You for any help/advice given:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂ガ小子 2024-08-29 19:30:13

你很接近了。您现在需要做的就是启动运行循环,这样线程就不会退出并且计时器会运行。在调用 initWithTimerInterval: 之后,只需调用

[[NSRunLoop currentRunLoop] run];

线程将无限期地运行其运行循环,并且您的计时器将工作。

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.

最近可好 2024-08-29 19:30:13

听起来你可能想要一个 NSOperation 而不是老式线程。您可以通过通用计时器激活该操作,然后它将在自己的线程上执行,然后在完成后清理自己的内存。

It sounds like you might want an NSOperation instead of an old fashion thread. You could active the operation via a universal timer then it would execute on its own thread and then clean up its own memory when done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文