Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Watson1978 edited this page Nov 15, 2012 · 6 revisions

In order to have full access Grand Central Dispatch (GCD) APIs, MacRuby has the module and some classes.

Constants in Dispatch Module

Dispatch::TIME_NOW

Indicates a time that occurs immediately. This constant is defined in GCD API as DISPATCH_TIME_NOW.

gcdq = Dispatch::Queue.new('sample')
gcdq.after(Dispatch::TIME_NOW) { puts "Hello" }

sleep 0.5

Dispatch::TIME_FOREVER

Indicates a time that means infinity. This constant is defined in GCD API as DISPATCH_TIME_FOREVER.

gcdq = Dispatch::Queue.new('sample')
sema = Dispatch::Semaphore.new(0)
gcdq.async {
  puts "Hello, "
  sleep 1
  sema.signal
}

puts "Waiting..."

sema.wait(Dispatch::TIME_FOREVER)
puts "World"

Methods in Dispatch Module

Dispatch#resume!

Resume the invocation of suspended dispatch queue or dispatch event source objects on a dispatch object. This method is declared in GCD API as dispatch_resume.

  • resume!
gcdq = Dispatch::Queue.new('sample')
gcdq.after(Dispatch::TIME_NOW) { 
  sleep 0.5
  puts "Hello"
}
gcdq.suspend!
gcdq.suspended?  #=> true
gcdq.resume!

sleep 1

Dispatch#suspend!

Suspends the invocation of dispatch queue or dispatch event source objects on a dispatch object. This method is declared in GCD API as dispatch_suspend.

  • suspend!

Dispatch#suspended?

Returns whether the invocation is suspended.

  • suspended? -> bool
    • [RETURN]
      • Returns a true if suspended, otherwise false.

Dispatch#dispatch_object

Returns the object of dispatch_object_t type for passing into Cocoa APIs which need the object of dispatch_queue_t type and etc.

  • dispatch_object -> object
    • [RETURN]
      • Returns a dispatch object
gcdq = Dispatch::Queue.new('doc')
output = AVCaptureVideoDataOutput.new
output.setSampleBufferDelegate(self, queue: gcdq.dispatch_object)
Clone this wiki locally