0

i have four operations to be done as follows

  1. start actvityindicator.
  2. do some caclulations. (in bg thread)
  3. save the results in xml .(in bg thread)
  4. stop the actvityindicator.

Now I am doing these operations in GCD as follows.

[self showAlert];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    [self calculateValues];

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        [utility createXMLWithName:name];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [self hideAlert];
        });
    });
});

I want to confirm is this the right way to do this in GCD? I need the task 2 and 3 in bg and also task 3 should happen only after task 2 finishes. For that i put the task 2 and 3 in separate queues.

4

2 に答える 2

2

i think it will work but i am not sure this is the best approach. i have posted some article about concurrency programing you might find it interesting for you question , you could have a look. concurrency programming in objective C

in general if you would like to be sure you run your thread only after other thread is finish his work you can use:
[NSObject performSelector: onThread: withObject: waintUntilDone:YES]; .

(you could use NSOperation but it seems like overhead for what you need )

but in your case i don't really understand why you need the second background thread for task 3, if you want to run it only when task 2 finish. couldn't you just right task 2 and then 3.

于 2012-11-22T11:33:32.890 に答える
1

Yes, this is fine. Just be aware that if your app is consistently active in the normal or high-priority queues, your createXMLWithName: calls may be delayed substantially, and you risk building up a backlog of save operations. Unless you witness an actual performance issue, it might be safest to just dispatch a single block to the normal priority queue that calculates and saves your data.

于 2012-11-22T17:38:53.107 に答える