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
I think I tracked down the issue. It was because I was declaring url in the method
def foo
url = "www.google.com"
Dispatch
take action on url
end
end
By moving the url declaration in to the Dispatch thread, it works. I think it was a matter of a method local variable going out of scope before the task had time to execute.
def foo
Dispatch
url = "www.google.com"
take action on url
end
end