NOrder
NOrder

Reputation: 2493

cocoa: using dispatch_async error

I'm using Oomph mapkit in my project. My code is:

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

this just convert a point to latitude and longitude. If I use dispatch_sync, it can run correctly. but I I use dispatch_async, the program will crash.

The error:

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

please help me.

Upvotes: 0

Views: 332

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

You can't access UIViews from a background thread without risking crashes. Since self.mapView is a UIView, it's not safe to access it from a block running in your async dispatch queue.

To do a bulk version of this on the main thread, you'll want to break this up into many smaller operations. It's probably easiest to create an NSBlockOperation that takes a list of, say, 100 points to convert, and create as many of those operations as you need for your entire list. You can then queue those on [NSOperationQueue mainQueue] for execution on the main thread.

Upvotes: 3

Related Questions