简体   繁体   中英

sqlite-net and Async methods in xamarin forms

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#?

If you want to keep the calling method not async, you have a few option

First is the easiest, just get the Result property from Task, but this can result in deadlocks, blocking the current thread until the result is returned, which is bad. You also have another option, which is to wrap the call in a Task.Run() then get Result from that instead, this will query the Task onto another thread, so no blocking.

There's no reason you can't make the calling method async, and if you made a method async, usually everything else on top of it (calling it) should be async too.

The reason why we use async is to avoid blocking, usually in a UI app like Xamarin.Forms or WPF, for example to allow stuff like put a loading screen or even animation on the screen while loading database, or still let user work on other part of the app while this part is loading.

If you don't need that, then just use the Synchronous API. But providing that you are making a Xamarin app, aka mobile app, responsiveness is very important, you don't want an app that's taking the whole screen to just hang without any indication that it's still working, and working hard at that.

Consider watching or reading some tutorial on async for more information on it.

Your code about getting data from sqlite database, I think it is correct, but you can also try the following code.

public async Task<List<Energys>> GetAlldata()
{
    var data = await conn.Table<Energys>().ToListAsync();
    return data;
}

private async void GetDataAsync()
{
    List<Energys> list = await GetAlldata();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM