为什么我的HTTP请求(冒泡包装)无法在Grand Central Dispatch队列中执行?

时间:2012-11-02 19:57:38

标签: http asynchronous grand-central-dispatch rubymotion

我在RubyMotion应用程序中执行此操作:

Dispatch::Queue.concurrent('google').async {
  BubbleWrap::HTTP.get("http://google.com") do |response|
    p response.body.to_str
  end
}

此通话未完成。

但是,如果我在Dispatch队列之外使用BubbleWrap代码,它就可以了。

1 个答案:

答案 0 :(得分:4)

BubbleWrap::HTTP是对NSURLConnection的抽象,它依赖于RunLoop进行异步处理。不幸的是,RunLoops没有设置在GCD队列上,我还没想出如何在GCD队列上启动runloop。当我需要上述内容时,我使用了实例化新的NSThread,并手动启动了运行循环:

action = lambda do
  runLoop = NSRunLoop.currentRunLoop

  BW::HTTP.get("http://www.google.com") do |response|
    NSLog("Fetched Google!")
  end

  runLoop.run
end

thread = NSThread.alloc.initWithTarget action, selector:"call", object:nil
thread.start

此答案是对my blogpost on the same topicCocoa In The Shell

的重复