Asynchronous calls with sqlite-net-pcl

Lydecker 1 Reputation point
2021-02-15T14:54:51.627+00:00

Hi there,

I am using sqlite-net-pcl with Xamarin Forms, and have an app fully working.

I am, however, worried that I am potentially using asynchronous commands (or not using them) incorrectly.

I followed the guide here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases

Here it states you create a table with await + ConfigAwait(false) in an async function call like so:

private async Task InitializeAsync()  
{    
await _db.CreateTablesAsync(CreateFlags.None, typeof(Models.AppPayload.Setting)).ConfigureAwait(false);    
}  

HOWEVER, when they start doing data manipulation to retrieve data, they don't use await + ConfigAwait(false) in an async function call.

They do this:

public Task<Models.AppPayload.Setting> GetSettingValue(string key)  
 {  
return _db.Table<Models.AppPayload.Setting>().Where(i => i.Key == key).FirstOrDefaultAsync();  
 }  

Instead of this:

public async Task<Models.AppPayload.Setting> GetSettingValue(string key)  
{  
return await _db.Table<Models.AppPayload.Setting>().Where(i => i.Key == key).FirstOrDefaultAsync().ConfigureAwait(false);   
 }  

Is there any difference between the two techniques, or is there any specific reason why the author has not made their function call 'async' like the bottom method?

Thanks for your help

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,347 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,772 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Joe Manke 1,091 Reputation points
    2021-02-15T22:06:15.013+00:00

    Probably because they're calling InitializeAsync from the constructor, which is not really a good idea in the first place.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.