iOS MKMapShapshotter完成块并不总是被调用

我试图使用新的iOS7 MKMapSnapshotter来生成静态地图图像.每当我的应用程序需要一张地图时,我打电话给以下内容:
MKMapSnapshotter *snapshotter = [[[MKMapSnapshotter alloc] initWithOptions:theOptions] autorelease];
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0);
DebugLog(@"Snapshotter allocated %@ and run on queue %@",snapshotter,aQueue);

[snapshotter startWithQueue:aQueue completionHandler:^(MKMapSnapshot *snapshot,NSError *error) {
    DebugLog(@"Snapshotter completion block %@",snapshotter);
    // perform selector on main thread to set self.imageView.image = shanpshot.image;
}

在大多数情况下,这是非常好的.然而有时候,似乎设备会因地图请求而重载,然后停止渲染.在我的日志文件中,我会看到关于“分配的Snapshotter”的第一个日志语句,但是从不看到“Snapshotter完成块”消息.

我的请求是否可能永远不会执行从调度队列?
有没有人有这个问题?

解决方法

这是(或者似乎是)MKMapSnapshotter中的一个错误.

如果网络数据和WiFi关闭,则不会调用完成处理程序(除非操作系统中有缓存的数据 – 请参阅https://stackoverflow.com/a/5769108/481207清除缓存).

实际上,快照器似乎阻止等待数据.它没有超时或检测到没有数据.几分钟后,例如15分钟,snapshotter.isLoading = YES.调用cancel不会导致完成处理程序被调用.

如果WiFi或网络数据被重新打开,后续的启动(新)快照的调用将调用完成处理程序.

如果在处理程序中启动和清除快照程序时设置了变量,那么这个错误会很严重,因为该变量永远不会被清除.

if (!isRendering) {
    isRendering = YES;

    [snapshotter startWithCompletionHandler:
     ^(MKMapSnapshot* snapshot,NSError* error) {
         // This may not be called so this code will
         // never run again.
         isRendering = NO;
     }];
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...