Dou*_*ith 3 objective-c grand-central-dispatch ios
我想要一个包含dispatch_after语句的for循环.问题是dispatch_after调用似乎与for循环不一致.换句话说,我希望它只在dispatch_after块中的语句执行后才开始for循环的下一次迭代.
我该怎么做?
我想在屏幕上呈现文字.传统上我每秒显示一个单词.但是根据单词的长度,我现在想要显示更长的单词,稍长的单词,以及更短的单词.我想提出一个词,等一会儿(取决于这个词有多长)然后呈现下一个词,等一会儿,然后是下一个,等等.
打印0,1,2,3,4,5,6,7,8,9,每秒一位数.
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0UL);
for (int i=0; i<10; i++) {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(queue,^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1LL * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
NSLog(@"%d",i);
dispatch_sync(dispatch_get_main_queue(), ^{
// show label on screen
});
dispatch_semaphore_signal(semaphore);
});
});
}
Run Code Online (Sandbox Code Playgroud)
如果您陈述您的用例,也许还有其他方法可以完成您要做的事情.
您还可以提前累积延迟时间并发送所有块.
(1) __block double delay = 0;
(2) dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0UL);
(3) for (int i=0; i<10; i++) {
(4) delay += 1LL * NSEC_PER_SEC; // replace 1 second with something related to the length of your word
(5) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay), queue, ^{
NSLog(@"%d",i);
(6) dispatch_sync(dispatch_get_main_queue(), ^{
// show label on screen
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是实现这一目标的一种方法。当然,您需要将我的 NSLog 替换为显示单词的代码,并将我的简单0.05 * word.length函数替换为您用来确定延迟的任何函数,但这应该可以解决问题,并且不会阻塞呈现线程。
- (void)presentWord: (NSString*)word
{
// Create a private, serial queue. This will preserve the ordering of the words
static dispatch_queue_t wordQueue = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
wordQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
});
dispatch_async(wordQueue, ^{
// Suspend the queue
dispatch_suspend(wordQueue);
// Show the word...
NSLog(@"Now showing word: %@", word);
// Calculate the delay until the next word should be shown...
const NSTimeInterval timeToShow = 0.05 * word.length; // Or whatever your delay function is...
// Have dispatch_after call us after that amount of time to resume the wordQueue.
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeToShow * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
dispatch_resume(wordQueue);
});
});
}
// There's nothing special here. Just split up a longer string into words, and pass them
// to presentWord: one at a time.
- (void)presentSentence: (NSString*)string
{
NSArray* components = [string componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[components enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
[self presentWord: obj];
}];
}
Run Code Online (Sandbox Code Playgroud)
编辑:它的工作方式是我使用串行队列来维护单词的顺序。当您向它提交一个单词时,它-presentWords会在 的“后面”排队一个块wordQueue。当该块开始执行时,您知道它wordQueue没有暂停(因为您位于正在执行的块中wordQueue),并且我们要做的第一件事就是暂停wordQueue。由于该块已经“正在运行”,它将运行直至完成,但在有人恢复它之前不会运行其他块wordQueue。暂停队列后,我们显示该单词。它将一直显示,直到显示其他内容。然后,我们根据刚刚开始显示的单词的长度计算延迟,并设置一个在该时间过去后dispatch_after恢复的时间。wordQueue当串行队列恢复时,下一个字的块开始执行,挂起队列并重复整个过程。
| 归档时间: |
|
| 查看次数: |
3900 次 |
| 最近记录: |