I am doing some asynchronous data fetching, and I need to do this in several calls to database. All these tasks need to be executed in a predefined sequence, and for this purpose I am using coroutines and Task.
GetItemsA(); // wrapped in new Task
GetItemsB(); // wrapped in new Task
GetItemsC(); // wrapped in new Task
Currently I am creating a new Task for each of these calls. Is this a bad practice, and should I try to avoid this, and wrap all cals in a single task?
GetItems(); // wrap in single Task
void GetItems()
{
GetItemsA();
GetItemsB();
GetItemsC();
}
The good practice is usually to ignore overhead and focus on readability of your code. Only when you find out that some code is a bottleneck in your application, try to optimize it.
The overhead of creating a new
Taskwill be most likely negligible, especially when compared with a database roundtrip.