Tag: 大中央调度

是否有必要在GCD下创build一个autorelease池?

我有一个CAEAGLLayer的运行循环方法,它使用GCD序列化访问共享ivars。 我的绘图代码目前是这样构造的: – (void)draw { dispatch_sync(serialDrawingQueue, ^{ @autoreleasepool { [self drawingStart]; [spriteA draw]; [spriteB draw]; [self drawingEnd]; } }); } draw方法由CADisplayLink调用。 我使用GCD块时需要@autoreleasepool吗?

方法不调用dispatch_async并重复NSTimer

我正在开发一个应用程序,我想使用dispatch_async在单独的队列中调用方法。 我想在一定的时间间隔后反复调用这个方法。 但是这个方法没有被调用。 我不知道什么是错的。 这是我的代码: dispatch_async( NotificationQueue, ^{ NSLog(@"inside queue"); timer = [NSTimer scheduledTimerWithTimeInterval: 20.0 target: self selector: @selector(gettingNotification) userInfo: nil repeats: YES]; dispatch_async( dispatch_get_main_queue(), ^{ // Add code here to update the UI/send notifications based on the // results of the background processing }); }); -(void)gettingNotification { NSLog(@"calling method "); }

创build具有相同名称的串行队列的两个对象共享同一个队列

不确定的行为,因为我怀疑我正在陷入僵局, 我有一个类与多个对象 – 每个对象创build一个具有相同名称的队列。 我不确定GCD是否在对象之间重复使用相同的队列,或者它们是否共享相同的名称。 例如 @interface MyClass -(void)doSomeWork @property (nonatomic,strong) dispatch_queue_t myQueue; @end @implementation MyClass -(id)init { self = [super init]; self.myQueue = dispatch_queue_create("MyQueue",DISPATCH_QUEUE_SERIAL); return self; } -(void)doSomeWork { dispatch_async(self.myQueue,^{ // some long running work }); } @end @interface SomeClassWhichCreatesALotOfObjects @end @implementation SomeClassWhichCreatesALotOfObjects -(void)someMethod { for(int i = 0; i < 10000; i++) { […]

NSOperationqueue背景,下载图片

我创build了一个NSOperationQueue来下载图片(来自Twitter for Cell): NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [queue addOperationWithBlock:^{ NSString *ImagesUrl = [[NSString alloc]initWithFormat:@"http://api.twitter.com/1/users/profile_image/%@",[[status objectForKey:@"user"]objectForKey:@"screen_name"]];; NSURL *imageurl = [NSURL URLWithString:ImagesUrl]; UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]]; [[NSOperationQueue mainQueue]addOperationWithBlock:^{ if (img.size.width == 0 || [ImagesUrl isEqualToString:@"&lt;null&gt;"]) { [statusCell.imageCellTL setFrame:CGRectZero]; statusCell.imageCellTL.image = [UIImage imageNamed:@"Placeholder"] ; }else [statusCell.imageCellTL setImage:img]; 这工作正常,但当它似乎移动滚动和查看图像仍在加载,他们正在改变几次,直到你得到一张照片。 而且我不喜欢诊断时间轮廓,所以我想以某种方式在后台创build这个NSOperationQueue 也可以展示如何使一个“Imagecache”不需要下载已经下载的图像。 **(状态= Twitter时间轴的NSDictionary)。 编辑:(所有单元格) – (UITableViewCell […]

在iOS4设备上发送大量数据的最佳做法?

我有一个应用程序需要发送数据(使用POST)到服务器。 此function必须在其中一个NavigationController子控制器上,用户应该能够离开此控制器和/或closures应用程序(只支持iPhone4 / iOS4)。 我应该使用线程/ NSOperations还是/和使用现有的asynchronous方法发送数据? 任何想法/最佳实践如何实现这一点?

使用dispatch_async或performSelectorOnMainThread在主线程上执行UI更改?

可能重复: Grand Central Dispatch(GCD)与performSelector – 需要更好的解释 要在主线程上执行“stuff”,我应该使用dispatch_async还是performSelectorOnMainThread ? 有没有一个首选的方式,是/否,和/或最佳实践? 例如:我正在NSURLConnection sendAsynchronousRequest:urlRequest方法的块内执行一些逻辑。 因为我正在做的东西的主视图,如呈现一个UIAlertView我需要在主线程上显示UIAlertView 。 为此,我使用下面的代码。 [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { // code snipped out to keep this question short if(![NSThread isMainThread]) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops!" message:@"Some Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; }); } }]; 在同样的if(![NSThread […]

在dispatch_semaphore_dispose上的EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,子码= 0x0)

我在dispatch_semaphore_dispose上得到了EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0),但是并不知道如何find这个的根本原因。 我的代码使用dispatch_async,dispatch_group_enter等。 更新:崩溃的原因是因为webserviceCall(请参阅下面的代码)永远不会调用onCompletion,并再次运行代码时,我得到了错误EXC_BAD_INSTRUCTION。 我证实这确实是这样,但不知道为什么或如何防止这种情况。 码: dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_group_t group = dispatch_group_create(); for (…) { if (…) { dispatch_group_enter(group); dispatch_async(queue, ^{ [self webserviceCall:url onCompletion:^{ dispatch_group_leave(group); }]; }); } } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC))); dispatch_sync(queue, ^{ // call completion handler passed in by caller }); });

在应用程序委托中返回之前,等待asynchronous任务完成完成块

我正在使用UIManagedDocument的子类在我的项目中使用核心数据。 重点是子类返回一个单例实例,以便我的屏幕可以简单地调用它,并且托pipe对象上下文对于所有这些实例都保持相同。 在使用UIManagedDocument之前,我需要打开它,如果它的文件path已经存在,或者创build它,如果它尚未。 我创build了一个方便的方法prepareWithCompletionHandler:在子类中方便两种场景。 @implementation SPRManagedDocument // Singleton class method here. Then… – (void)prepareWithCompletionHandler:(void (^)(BOOL))completionHandler { __block BOOL successful; // _exists simply checks if the document exists at the given file path. if (self.exists) { [self openWithCompletionHandler:^(BOOL success) { successful = success; if (success) { if (self.documentState != UIDocumentStateNormal) { successful = NO; } } […]

Block_release在后台线程上取消分配UI对象

在WWDC 2010“块和大中央调度”演讲中提出的模式之一是使用嵌套的dispatch_async调用在后台线程上执行耗时的任务,然后在任务完成后更新主线程上的UI dispatch_async(backgroundQueue, ^{ // do something time consuming in background NSArray *results = ComputeBigKnarlyThingThatWouldBlockForAWhile(); // use results on the main thread dispatch_async(dispatch_get_main_queue(), ^{ [myViewController UpdateUiWithResults:results]; }); }); 由于在块内部使用了“myViewController”,它会自动得到一个“保留”,并在清理块之后得到一个“释放”。 如果块的“释放”调用是最终的释放调用(例如,用户在后台任务运行时离开视图),则调用myViewController dealloc方法 – 但是在后台线程调用它! UIKit对象不喜欢在主线程之外解除分配。 在我的情况下,UIWebView引发一个exception。 这个WWDC如何呈现模式 – 特别提到是避免UIlocking的最佳新方法 – 是如此的有缺陷? 我错过了什么吗?

在Grand Central Dispatch中使用dispatch_sync

任何人都可以用真正清楚的用例来解释GCD中dispatch_sync的用途是什么? 我不明白在哪里,为什么我不得不使用这个。 谢谢!