Shizam
Shizam

Reputation: 9672

Is a dispatch_group_notify block persistant for the lifespan of a dispatch_group_t?

If I have a dispatch_group class property:

@property (nonatomic, readonly) dispatch_group_t _serialGroup;

and I have a block that I always want called whenever the group completes:

dispatch_group_notify(self._serialGroup, self._serialQueue, ^{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //...do some stuff...
    });
});

Can I just define the dispatch_group_notify upon initilization of the dispatch_group once and it'll be called whenever that group completes or do I need to redefine it every time I add items to the group?

Upvotes: 2

Views: 2394

Answers (2)

das
das

Reputation: 3681

A dispatch_group_notify() block gets submitted exactly once to the specified queue the first time the group becomes empty after the API is called (or immediately if the group is already empty).

If you want a notification block to be invoked again, you must reinstall it (e.g. as part of the notification block itself, but be aware that it will be re-submitted right away if the group is still empty when you reinstall it).

Upvotes: 4

bolnad
bolnad

Reputation: 4583

you have to release it yourself by calling dispatch_release(). So you could hold onto it till and reuse it

Upvotes: 0

Related Questions