Tag: 大中央调度

在iOS上,你可以做一个同步networking请求(但不是在主线程),仍然进展callback(在一个单独的,非主线程)?

在iOS上,你可以做一个同步的networking请求(离开主线程)并获得进度callback(在一个单独的,非主线程)? 我有一个串行(一次一个操作)的后台队列,可以运行所有耗时的作业,而这些作业现在不需要完成。 我确实想要显示下载作业的进度。 它看起来不像你可以实例化一个NSURLConnection并configuration一个委托,启动同步连接,然后获取进度callback。 有没有办法在该背景队列上进行同步请求(同步,因为它后面的作业不会启动,直到完成),仍然得到setProgress:可以发送更新进度条的callback? (callback将不得不在一个不同的队列线程,因为我的串行队列的线程被阻塞,直到请求完成。) 苹果的NSURLConnection文档说,同步请求实际上build立在asynchronous的幕后。 我必须重新实施吗? 我需要一种方法来阻塞线程,直到请求完成/失败。 到目前为止,最好的线索是NSOperationQueue的waitUntilFinished方法,但我不想开始asynchronous并持续轮询同步方法。 NSURLConnection讨论 一个同步加载是build立在该类可用的asynchronous加载代码之上的。 调用线程在asynchronous加载系统对专门为此加载请求生成的线程执行URL加载时被阻塞。 在调用线程中不需要特殊的线程或运行循环configuration来执行同步加载。

GCD和外部线程

有没有办法可以创build一个新的(或关联一个现有的)调度队列,并将其绑定到特定的线程? 我有一个AudioUnitcallback过程运行在我不控制的线程上,并且希望在执行callback之前检查给定的队列是否有任何阻塞让我在该AudioUnit线程中进行处理。 我大概可以使用OSAmtomicEnqueue和朋友,但是想知道GCD是否已经提供了某种“逃生”,允许我将特定的线程绑定到特定的队列?

iOS停止运行全局队列

码: dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [ServerAPI API_GetChatList:self withUserId:[self getUserIDFromUserDefaults] withScheduleId:strGroupId withType:@"group"]; dispatch_async(dispatch_get_main_queue(), ^{ [self performSelector:@selector(getChatList) withObject:nil afterDelay:10]; }); }); 我使用调度全局队列每10秒调用一个方法。它工作正常。问题是全局队列也继续在其他控制器上运行。如何阻止此运行?任何帮助将不胜感激。

用`dispatch_barrier`死锁

通过学习dispatch_barrier ,我写了一个例子如下: static dispatch_queue_t queue; static dispatch_queue_t readWriteLockQueue; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ queue = dispatch_queue_create("com.test.testasync", DISPATCH_QUEUE_CONCURRENT); readWriteLockQueue = dispatch_queue_create("com.test.readWriteLockQueueOfMeta", DISPATCH_QUEUE_CONCURRENT); }); for (NSInteger i=0; i<100; i++) { dispatch_async(queue, ^{ dispatch_sync(readWriteLockQueue, ^{ NSLog(@"read"); }); dispatch_barrier_sync(readWriteLockQueue, ^{ NSLog(@"write"); }); }); } dispatch_barrier_async(queue, ^{ NSLog(@"finished!"); }); 但是输出结果是: 2016-05-20 16:23:14.066 Test[23867:781553] read 2016-05-20 16:23:14.066 Test[23867:781555] read 2016-05-20 16:23:14.066 […]

如何重复循环dispatch_after语句?

我想有一个与他们的dispatch_after语句for循环。 问题是dispatch_after调用似乎不符合for循环。 换句话说,我希望它在dispatch_after块中的语句执行之后才开始for循环的下一次迭代。 我将如何做到这一点? 用例 我想在屏幕上显示文字。 传统上,我每秒显示一个字。 但是,根据单词的长度,我现在想要显示更长的单词稍长一点,短一点的单词稍微less一点时间。 我想提出一个词,稍等一会儿(取决于单词的长度),然后提出下一个单词,稍等一会儿,然后等下一个单词。

主线程在viewDidLoad中的一个并发队列上执行dispatch_async,或者在一个方法内部执行

所以在一些帮助下,我更清楚地知道嵌套的GCD在我的程序中是如何工作的。 原帖是: 确保我正确地解释嵌套的GCD 但是,您不需要阅读原始文章,但基本上这里的代码在后台运行数据库执行,并且UI是响应式的: -(void)viewDidLoad { dispatch_queue_t concurrencyQueue = dispatch_queue_create("com.epam.halo.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_queue_t serialQueue = dispatch_queue_create("com.epam.halo.queue2", DISPATCH_QUEUE_SERIAL); for ( int i = 0; i < 10; i++) { dispatch_async(concurrencyQueue, ^() { NSLog(@"START insertion method%d <–", i); dispatch_sync(serialQueue, ^() { //this is to simulate writing to database NSLog(@"———-START %d———", i); [NSThread sleepForTimeInterval:1.0f]; NSLog(@"——–FINISHED %d——–", i); }); NSLog(@"END […]

dispatch_set_target_queue如何工作?

由于dispatch_set_target_queue缺less资料,我来这里寻求帮助,谢谢! 这是我的testing代码: dispatch_queue_t mySerialDispatchQueue1 = dispatch_queue_create("come.itenyh", NULL); dispatch_queue_t mySerialDispatchQueue2 = dispatch_queue_create("come.itenyh1", NULL); dispatch_set_target_queue(mySerialDispatchQueue1, mySerialDispatchQueue2); dispatch_async(mySerialDispatchQueue1, ^{[self task:@"s1"];}); dispatch_async(mySerialDispatchQueue2, ^{[self task:@"p1"];}); dispatch_async(mySerialDispatchQueue1, ^{[self task:@"s2"];}); – (void)task:(NSString *)taskid { NSLog(@"Now executing taskid:%@", taskid); [NSThread sleepForTimeInterval:5]; } 现在,如果我设置 dispatch_set_target_queue(mySerialDispatchQueue2, mySerialDispatchQueue1); 那么结果是: 2014-04-16 22:23:49.581 ITGCDLearning[66758:1303] Now executing taskid:s1 2014-04-16 22:23:54.585 ITGCDLearning[66758:1303] Now executing taskid:s2 2014-04-16 22:23:59.586 ITGCDLearning[66758:1303] Now executing […]

全局队列中的定时器不在iOS中调用

-(void)viewDidLoad{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(action_Timer) userInfo:nil repeats:YES]; } ); } -(void)action_Timer{ LOG("Timer called"); } action_Timer没有被调用。 我不知道为什么。 你有什么主意吗?

与GCD重用UITableViewCell

我正在使用Grand Central Dispatch来asynchronous加载UITableViewCell图像。 除了在某些边框情况下单元格被重用,以及之前的块加载错误的图像之外,这种方法效果很好。 我目前的代码如下所示: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } NSString *imagePath = [self imagePathForIndexPath:indexPath]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); dispatch_async(queue, ^{ UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; dispatch_sync(dispatch_get_main_queue(), ^{ cell.imageView.image = image; […]

如何在特定的线程上locking一个NSLock

我有一个属性@property NSLock *myLock 我想写两个方法: – (void) lock 和 – (void) unlock 这些方法分别locking和解锁myLock ,无论调用哪个线程或队列,都需要这样做。 例如,线程A可能调用了lock但是队列B可能是调用unlock那个。 这两种方法都应该正常工作,而不报告我正试图解锁locking它的不同线程/队列的锁。 此外,他们需要同步做到这一点。