Padin215
Padin215

Reputation: 7484

Checking if a thread (using GCD) exists or has finished

I would like to be able to download some data from our server in the background after they log in. Is there a way I can set it up so when the user goes to that section of the program (say its downloading recipes), that I can check if there is a current download for a new recipe that hasn't saved yet?

It would be nice if the user goes to recipes, and sees an indicator of some sort that it is currently downloading new recipes.

Can I name the threads? Can I put all the "recipe" threads in stack and check if its empty or has a thread in it?

I'm using GCD.

Any ideas?

Upvotes: 2

Views: 1155

Answers (2)

Rob
Rob

Reputation: 437917

Rather than checking for the state of dispatch queues, I would suggest that your model have a property that indicates the state of the downloads. You can have your GCD code change the state to "downloading" when the download commences, and then change that state back to something else when the download is done. Because you appear to have multiple view controllers that want to interact with this background download operation, you might want to use local notifications (or KVN) so that any view dependent upon the state of the model can update itself at the appropriate times.

If you use operation queues instead of dispatch queues (which is useful for other reasons, too, such as you want concurrent downloads but want to limit this to some reasonable number), you could theoretically use operationCount, but I'd still be inclined to maintain a download status model property as outlined above.

Upvotes: 3

Jeremy
Jeremy

Reputation: 9030

This is what I would do if using GCD (not tested, but the lesson to be gleaned is that you can use dispatch_group_notify when your download has completed in combination with a global flag to indicate the state of the download process).

-(void)downloadRecipes
{
    //<--Start activity indicator-->
    //isDownloading = YES; //<- Some global flag to indicate download in progress
    //--or--
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidBegin"];
    dispatch_block_t executionBlock = 
        ^{
              //Your download logic here
         };
    dispatch_block_t executionBlock_OnComplete =
        ^{
             //isDownloading = NO; //<- Global flag indicates no longer downloading
             //--or--
             //[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidEnd"];
             //<--Stop activity indicator-->
         };
    dispatch_queue_t someQueue = dispatch_queue_create("com.myapp.recipe.download", NULL);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, someQueue, executionBlock);
    dispatch_group_notify(group, dispatch_get_main_queue(), executionBlock_OnComplete);
    dispatch_release(group);
    dispatch_release(queue);
}

Optionally, instead of setting a global flag, you can use NSNotificationCenter to post a notification when the process has begun as well as when it has completed.

Upvotes: 3

Related Questions