As stated in the title of this question, I am using sqlite-net nuget package in a xamarin forms shell application. I have chosen to use the Async version of the sqlite API in my app. I have developed a method, but I'm not actually sure if this is correct. Technically it does work, and it does what I want it to do, but I was wondering if this is really the best way to do it. Here is the method in question
public async static Task<List<BPEntry>> GetAllBP()
{
var query = database.Table<BPEntry>();
var result = await query.ToListAsync();
return result;
}
The part that I'm confused with is that, when you use the await operator on the query.ToListAsync()
it gives me back a List<BPEntry>
object.
Now, I want to return that List<BPEntry>
object to the calling method, but because of the way that I understand async methods work, I can't just pass back the List<BPEntry>
directly.
Instead I'm forced to return a Task
that contains the list. Which means I will also have to make my calling method I as well.
This just seems strange to me. Why wouldn't i be able to just pass back the List<BPEntry>
object directly to a non-async calling method?
It's almost as if you unpack the operation to get a List<BPEntry>
but then instead of passing it back directly, you have to wrap it back up in a task. Which seems redundant to me.
So am i doing this correctly?
Is there something I'm missing?
Or this just how async methods work in C#?