Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rubymotion Dispatch exiting early

Tags:

rubymotion

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
like image 985
nathasm Avatar asked Mar 04 '26 06:03

nathasm


1 Answers

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
like image 112
Mason Cloud Avatar answered Mar 06 '26 03:03

Mason Cloud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!