首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NSInvocationOperation回调太快了

NSInvocationOperation回调太快了
EN

Stack Overflow用户
提问于 2012-11-16 11:45:25
回答 1查看 452关注 0票数 5

我知道类似的问题曾被问过几次,但我很难弄清楚这个问题是如何解决的。到目前为止,我所做的一切都是以最重要的方式完成的。现在,我发现我需要执行一个需要一些时间的操作,我希望在操作期间向我的显示器添加一个HUD,并在操作完成后将其淡出。

在阅读了大量关于GCD的内容(并且非常困惑)之后,我决定最简单的方法是使用NSInvocationOperation调用我的耗时方法,并将其添加到新创建的NSOperationQueue中。这就是我所拥有的:

代码语言:javascript
运行
复制
        [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];

我希望这能显示HUD,开始在地图上绘制点,然后在操作完成后,淡出HUD。

相反,它显示HUD,开始在地图上绘制点,在绘制点的同时逐渐淡出HUD。根据我的NSLogs,在调用该方法淡出HUD之前,大约有四分之一秒的延迟。与此同时,这些点的绘制又持续了几秒钟。

我怎样才能让它等到地图上的绘图完成后,才能褪色HUD呢?

谢谢

编辑后添加:

在做了以下修改之后,我几乎获得了成功:

代码语言:javascript
运行
复制
        NSInvocationOperation *showHud = [[NSInvocationOperation alloc] initWithTarget:self
                                                                              selector:@selector(showLoadingConfirmation)
                                                                                object:nil];

        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                                selector:@selector(timeConsumingOperation:)
                                                                                  object:[self lotsOfDataFromManagedObject]];

        NSInvocationOperation *hideHud = [[NSInvocationOperation alloc] initWithTarget:self
                                                                              selector:@selector(fadeConfirmation:)
                                                                                object:loadingView];

        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];

        NSArray *operations = [NSArray arrayWithObjects:showHud, operation, hideHud, nil];
        [operationQueue addOperations:operations waitUntilFinished:YES];

奇怪的是,它似乎先调用timeConsumingOperation,然后调用showLoadingConfirmation,然后调用fadeConfirmation。这是根据我的NSLogs,这是在这些方法中激发的。

我在屏幕上看到的行为是这样的:点被绘制,地图相应地调整它的缩放(timeConsumingOperation的一部分),然后HUD出现在屏幕上,然后什么也没有。这三个NSLogs都会立即出现,尽管showLoadingConfirmation在timeConsumingOperation完成之前不会发生,而fadeConfirmation似乎根本不会发生。

这似乎很奇怪,但也似乎表明,在timeConsumingOperation完成时,有一种方法可以使某些事情发生。

我试着加了这个:

代码语言:javascript
运行
复制
[operationQueue setMaxConcurrentOperationCount:1];

此外,这也是:

代码语言:javascript
运行
复制
[showHud setQueuePriority:NSOperationQueuePriorityVeryHigh];
[operation setQueuePriority:NSOperationQueuePriorityNormal];
[hideHud setQueuePriority:NSOperationQueuePriorityVeryLow];

但它们似乎没有什么区别。

EN

回答 1

Stack Overflow用户

发布于 2012-11-19 14:50:41

如果HUD正在消失,则调用完成块,这意味着您的timeConsumingOperation:方法返回。

如果您仍然看到点正在绘制,这意味着动画或绘图事务仍在进行中或仍在排队,即使在timeConsumingOperation:返回之后。解决方案取决于您所使用的绘图技术。你在使用核心动画吗?带注释的MapKit?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13415957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档