简体   繁体   English

iPhone SDK - 在后台线程中运行重复进程

[英]iPhone SDK - Running a repeating process in a background thread

I have an iPhone application which in which I want to perform a method in the background every 1 second. 我有一个iPhone应用程序,我想在后台每1秒执行一次方法。

So in my main thread, I have the following code upon my UIViewController viewDidLoad() : 所以在我的主线程中,我在UIViewController viewDidLoad()有以下代码:

[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(repeatedMethod) userInfo:nil repeats:YES];

with the following methods: 使用以下方法:

-(void)repeatedMethod {
  if(!processIsRunning) {
    processIsRunning = YES;
    [self performSelectorInBackground:@selector(runProcessInBackground:) withObject:myImage];
  }
}

-(void)runProcessInBackground:(UIImage *)image {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  ... Some operations ...
  ...   also call a method on the main thread which sets isProcessRunning to NO

  [pool release]
}

The way I have this set up, a new thread is spawned every second (unless the process is still running, thanks to my processIsRunning flag). 我有这个设置的方式,每秒产生一个新线程(除非进程仍在运行,感谢我的processIsRunning标志)。

Now, my questions are: 现在,我的问题是:

(1) Is this the best way to do this? (1)这是最好的方法吗? Or is there a more appropriate way to retain and reuse the background thread? 或者是否有更合适的方法来保留和重用后台线程?

(2) What might be the fastest way to do this? (2)最快的方法是什么? Am I losing time by spinning up new background threads each time the method is called? 每次调用方法时,我是否会通过调整新的后台线程来浪费时间?

The code works fine as is, it's just a quite a bit slower when I ran everything on the main thread (which I ultimately don't want to do). 代码工作得很好,当我在主线程上运行所有东西时,它只是相当慢一点(我最终不想这样做)。

Any advice would be great! 任何建议都会很棒! Has anyone dealt with this type of question before? 有没有人以前处理过这类问题?

Many thanks, Brett 非常感谢,布雷特

Based on the question in your comment, I figured I'd expand my comment into a real answer. 根据您评论中的问题,我想我会将我的评论扩展为真正的答案。 You can use GCD for this, and it's likely best way to go. 您可以使用GCD,这可能是最佳方式。

Something like this: 像这样的东西:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 1.0*NSEC_PER_SEC, 0*NSEC_PER_SEC);
dispatch_source_set_event_handler(timerSource, ^{
    [self repeatedMethod];
});
dispatch_resume(timerSource);

- (void)repeatedMethod {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self doStuffWithAnImage:self.myImage];   

    [pool drain];
}

One of the coolest things about blocks is that they capture local scope. 关于块的最酷的事情之一是它们捕获局部范围。 That's not actually used in my example above, but it makes "passing" objects into GCD trivial. 这实际上并没有在我上面的例子中使用,但它使“传递”对象变成GCD琐碎。 See my answer to this question: Is there an advantage to using blocks over functions in Objective-C? 请参阅我对这个问题的回答: 在Objective-C中使用块而不是函数是否有优势? .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM