Domanda

I have 10 instances of a class which need to do some background tasks. This should happen serially for the instances, but could be concurrent in regards to that the instances can do work independent of each other.

Which is most cost effective in terms of speed and battery? When do I need to be concerned that I've created too many queues?

This one (A) ?

- (dispatch_queue_t)queue
{
    static dispatch_queue_t queue;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        queue = dispatch_queue_create("no.agens.someclass.somequeue", DISPATCH_QUEUE_SERIAL);
    });
    return queue;
}

Or this one (B) ?

// assume read-only property of 'queue' is defined

- (dispatch_queue_t)queue
{
    if(_queue == nil)
    {
        _queue = dispatch_queue_create("no.agens.someclass.somequeue", DISPATCH_QUEUE_SERIAL);
    }
    return _queue;
}
È stato utile?

Soluzione

You want (B). (A) will create a single queue, and will serialize work across ALL instances, instead of just per instance. Queues are pretty lightweight. Unless this is a class you're going to have thousands of instances of at once, I wouldn't worry about the overhead of having a queue per instance.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top