是否可以将NSUrlConnection包装在gcd样式块中并在low_priority队列上运行?
我需要确保我的连接不在主线程上发生,并且连接需要是异步的.我还需要几个同时发出的请求.
如果我去gcd路由,我不确定调用NSUrlConnectionDelegate方法的哪个线程.
NSURLConnection依赖于委托,所以一旦连接完成,处理它的任何包装类都需要调用它的调用者.我不确定如何处理连接工作启动/完成时调用的所有各种回调:
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)incrementalData;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
Run Code Online (Sandbox Code Playgroud)
我应该只调用同步版本但是包含在gcd块中吗?如果我想取消通话,请使用'dispatch_suspend'?
dispatch_async(queue,^{
NSString* result = [self mySynchronousHttp:someURLToInvoke];
});
// If I need to cancel
dispatch_suspend(queue);
Run Code Online (Sandbox Code Playgroud) 我知道有几次问过类似的问题,但是我很难理解这个特殊问题是如何解决的.到目前为止,我所做的一切都是在主要方面进行的.我现在发现我需要执行一个需要一些时间的操作,并且我希望在操作期间向我的显示器添加HUD并在操作完成时将其淡出.
在阅读了很多关于GCD(并且变得非常困惑)之后,我决定最简单的方法是使用NSInvocationOperation调用我耗时的方法并将其添加到新创建的NSOperationQueue中.这就是我所拥有的:
[self showLoadingConfirmation]; // puts HUD on screen
// this bit takes a while to draw a large number of dots on a MKMapView
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(timeConsumingOperation:)
object:[self lotsOfDataFromManagedObject]];
// this fades the HUD away and removes it from the superview
[operation setCompletionBlock:^{ [self performSelectorOnMainThread:@selector(fadeConfirmation:) withObject:loadingView waitUntilDone:YES]; }];
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:operation];
Run Code Online (Sandbox Code Playgroud)
我希望这能显示HUD,开始在地图上绘制点,然后一旦完成该操作,就会消除HUD.
相反,它显示HUD,开始在地图上绘制点,并在仍然绘制点的同时淡化HUD.根据我的NSLogs,在调用淡入HUD的方法之前,有大约四分之一秒的延迟.与此同时,点的绘制持续了几秒钟.
我可以做些什么让它等到地图上的绘图完成后才会消失HUD?
谢谢
编辑添加:
在进行以下更改后,我几乎成功了:
NSInvocationOperation *showHud = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(showLoadingConfirmation)
object:nil];
NSInvocationOperation *operation = …Run Code Online (Sandbox Code Playgroud) nsoperation nsoperationqueue nsinvocation ios nsinvocationoperation
我有UITableView,它显示在我的图层`[[CCDirector sharedDirector] .view addSubview:myTableView]前面并占据了屏幕的某些部分.
一切正常:它检测到触摸.但是在tableView的滚动动画期间,所有cocos2d冻结,直到tableView结束此滚动.(在冻结下我的意思是所有节点的动作暂停).
问题是:这种冻结的原因是什么,我该如何避免?
-(void) mainLoop:(id)sender
{
if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) != 0)
{
return;
}
if (self.useCocoaFriendlyRendering)
{
dispatch_sync(frameRenderingDispatchQueue, ^{ // HERE dispatch_async make black screen, i shows everything but still bad performance.
CCGLView *openGLview = (CCGLView*)[self view];
[EAGLContext setCurrentContext:openGLview.context];
[self drawScene];
dispatch_semaphore_signal(frameRenderingSemaphore);
});
}
else
{
CCGLView *openGLview = (CCGLView*)[self view];
[EAGLContext setCurrentContext:openGLview.context];
[self drawScene];
dispatch_semaphore_signal(frameRenderingSemaphore);
}
}
Run Code Online (Sandbox Code Playgroud)