What is the iOS (or RubyMotion) idiom for waiting on a block that executes asynchronously?

后端 未结 4 1170
醉梦人生
醉梦人生 2021-01-03 12:59

I have been pulling my hair out for weeks on this niggling problem, and I just can\'t find any info or tips on how or what to do, so I\'m hoping someone here on the RubyMoti

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-03 13:26

    Here's what I do to make multi-threaded synchronized asynchronous calls.

    def make_sync2(&block)
      @semaphore ||= Dispatch::Semaphore.new(0)
      @semaphore2 ||= Dispatch::Semaphore.new(1)
      BubbleWrap::Reactor.schedule do
        result = block.call("Mateus")
        @semaphore2.wait # Wait for access to @result
        @result = result
        @semaphore.signal
      end
      @semaphore.wait # Wait for async task to complete
      result = @result
      @semaphore2.signal
      result
    end
    

提交回复
热议问题