0

我在我的项目中使用 Oomph mapkit。我的代码是:

dispatch_queue_t pQueue = dispatch_queue_create("pQueue", NULL);
dispatch_async(pQueue, ^(void){
    CLLocationCoordinate2D coordinate= [self.mapView convertPoint:point toCoordinateFromView:self];
});

这只是将一个点转换为纬度和经度。如果我使用dispatch_sync,它可以正常运行。但是二用dispatch_async,程序会崩溃。

错误:

1   0x7fff91c6e067 WTF::Vector<JSC::Identifier, 64ul>::shrinkCapacity(unsigned long)
2   0x7fff91c6df5e JSC::ParserArena::reset()
3   0x7fff91d881ea JSC::ScopeNode::destroyData()
4   0x7fff91d87b3d JSC::FunctionExecutable::produceCodeBlockFor(JSC::ScopeChainNode*,     JSC::CompilationKind, JSC::CodeSpecializationKind, JSC::JSObject*&)
5   0x7fff91d8751c JSC::FunctionExecutable::compileForCallInternal(JSC::ExecState*, JSC::ScopeChainNode*, JSC::JITCode::JITType)
6   0x7fff91c75a84 JSC::Interpreter::executeCall(JSC::ExecState*, JSC::JSObject*, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
7   0x7fff91c75924 JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
8   0x7fff8e7eac76 WebCore::JSMainThreadExecState::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&)
9   0x7fff8e0c71f2 -[WebScriptObject callWebScriptMethod:withArguments:]
10  0x100090bda -[MKMapView convertPoint:toCoordinateFromView:]
11  0x100033fa7 __51-[MKMapView(MKGeometryExtensions) clusterAnimated:]_block_invoke_0
12  0x7fff8da81f3d _dispatch_call_block_and_release
13  0x7fff8da7e0fa _dispatch_client_callout
14  0x7fff8da7f4c3 _dispatch_queue_drain
15  0x7fff8da7f335 _dispatch_queue_invoke
16  0x7fff8da7f207 _dispatch_worker_thread2
17  0x7fff893b1ceb _pthread_wqthread
18  0x7fff8939c1b1 start_wqthread

请帮我。

4

1 回答 1

3

你不能从后台线程访问 UIViews 而不会有崩溃的风险。由于self.mapView是 UIView,因此从运行在异步调度队列中的块访问它是不安全的。

要在主线程上执行此操作的批量版本,您需要将其分解为许多较小的操作。创建一个 NSBlockOperation 可能是最简单的,它需要一个包含 100 个点的列表来转换,并为整个列表创建尽可能多的这些操作。然后,您可以在 [NSOperationQueue mainQueue] 上排队,以便在主线程上执行。

于 2012-08-15T20:52:02.077 回答