I'm executing a fairly vanilla dispatch queue in Rubymotion, however it is apparently exiting early. It never gets past the initWithContentsOfURL call. However, removing the Dispatch::Queue wrapper and putting the calls in the main thread works.
The application in the simulator exits with no stack trace or indication of what went wrong. Am I mis-using the dispatch queue?
def foo
Dispatch::Queue.concurrent.async do
error_ptr = Pointer.new(:object)
data = NSData.alloc.initWithContentsOfURL(
NSURL.URLWithString(url), options:NSDataReadingUncached, error:error_ptr)
unless data
p error_ptr[0]
return
end
json = NSJSONSerialization.JSONObjectWithData(data, options:0, error:error_ptr)
unless json
presentError error_ptr[0]
return
end
Dispatch::Queue.main.sync { print_results(json) }
end
end
def print_results(json)
p "#{json}"
end
Right now it seems that RubyMotion does not properly retain the local variables outside the dispatch block, so is probably getting an EXEC_BAD_ACCESS and crashing. The following fails:
foo = "some value"
Dispatch::Queue.concurrent.async do
puts foo
end
However the following two will work:
@foo = "some value"
Dispatch::Queue.concurrent.async do
puts @foo
end
and also:
foo = "some value"
foo.retain
Dispatch::Queue.concurrent.async do
puts foo
foo.release
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With