我知道有几次问过类似的问题,但是我很难理解这个特殊问题是如何解决的.到目前为止,我所做的一切都是在主要方面进行的.我现在发现我需要执行一个需要一些时间的操作,并且我希望在操作期间向我的显示器添加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
我的代码突然无法在Xcode 6.1中编译(我确信它在Xcode 6 GM和beta版本中工作).它显示错误消息:
'NSInvocationOperation'不可用
我的代码是:
let operation = NSInvocationOperation(target:self, selector:"backgroundRun:", object:self)
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢.
我在我的应用程序中使用NSOperationQueue,我想为我的操作设置多个参数,我该怎么做?
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall) object:nil];
[queue addOperation:operation];
[operation release];
Run Code Online (Sandbox Code Playgroud) iphone cocoa-touch nsoperation nsoperationqueue nsinvocationoperation