不使用块的dispatch_async [英] dispatch_async without using a block
问题描述
我想偶尔使用dispatch_async从我的Web服务器中获取变化的资源(大约每60秒一次).
I want to use dispatch_async to occasionally pull a changing resource from my web server (about every 60 seconds).
我想使用dispatch_async,但是我想给它一个函数名.由于我希望该功能进入睡眠状态,然后再次重做同一件事.
I want to make use of dispatch_async but I want to give it a function name instead. Since I want that function to sleep and redo the same thing again.
这是我正在尝试做的事情dispatch_async(self.downloadQueue, @selector(getDataThread:));
(这不会编译)
This is what I am trying to do dispatch_async(self.downloadQueue, @selector(getDataThread:));
(this does not compile)
我认为while循环不是一个好主意,所以我想知道什么是最好的方法
I think a while loop is not a good idea so I'm wondering what was the best approach for this
推荐答案
类似...
NSTimer *timer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(getDataThread:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
- (void)getDataThread:(id)foo {
dispatch_async(self.downloadQueue, ^{
// code you want to run asynchronously
});
}
似乎您希望某些代码块异步运行...但是您希望它们也以一定的时间间隔运行,所以我认为这就足够了.
It seems you want some block of code to run asynchronously... but you want that to also run at a timed interval, so I think this should suffice.
如果您想完全放弃dispatch_async
,可以看一下NSOperationQueue
,请记住NSOperationQueue
是在GCD上构建的,但是您不必担心直接与它接口.
If you want to ditch blocks and dispatch_async
altogether you could look at NSOperationQueue
, keeping in mind NSOperationQueue
is built on GCD but you don't need to worry about interfacing with it directly.
这篇关于不使用块的dispatch_async的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!