Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Dispatch::Group Class

Watson1978 edited this page · 2 revisions

You could use the group to synchronize the dispatch queues which invoked for asynchronous.

Methods in Dispatch::Group class

Dispatch::Group.new

Creates a new group with which block objects can be associated.

  • new -> Group
    • [RETURN]
      • Returns a new group instance

Dispatch::Group#notify

Schedules a block object to be submitted to a queue when a group of previously submitted block objects have completed. This method is declared in GCD API as dispatch_group_notify.

  • notify(queue) { ... }
    • [PARAM] queue:
      • Specify a queue to which the supplied block is submitted when the group completes.
gcdq = Dispatch::Queue.new('sample')
gcdg = Dispatch::Group.new
@i = 0
gcdq.async { @i = 42 }
gcdg.notify(gcdq) { p @i } # => 42

Dispatch::Group#on_completion

This method is alias of Dispatch::Group#notify

Dispatch::Group#wait

Waits synchronously for the previously submitted block objects to complete. This method is declared in GCD API as dispatch_group_wait.

  • wait(timeout = Dispatch::TIME_FOREVER) -> bool
    • [PARAM] timeout:
      • Specify a value of timeout if needed. The default value is Dispatch::TIME_FOREVER.
    • [RETURN]
      • Returns a true if all completed, false if timed out.
gcdq = Dispatch::Queue.new('sample')
gcdg = Dispatch::Group.new
gcdq.async(gcdg) { sleep 5 }
gcdg.wait # => true

gcdq.async(gcdg) { sleep 5 }
gcdg.wait(1) # => false
Something went wrong with that request. Please try again.