Introduction
In mobile projects, whenever we do a parallel or concurrent task and insert data into multiple tables at the same time, then we need to implement locks on SQLite code. The reason behind this is SQLite is a file based database and cannot handle multiple calls to write in it. In Xamarin Forms we are using multiple platforms as Android, iOS and Windows and for this we need to implement dependency injection to initialize the SQLite DB.
We will start by interface first
The interface will have two functions, called "GetAsyncConnection" and "GetConnection". We will implement this function in respective projects as shown below.
- using SQLite.Net;
- using SQLite.Net.Async;
- namespace Sample.Mobile.Services.Common
- {
- public interface ISQLite
- {
- SQLiteAsyncConnection GetAsyncConnection();
- SQLiteConnection GetConnection();
- }
- }
In this we will implement the above interface functions by using dependency injection. In this we will store the DB File in APP folder itself. Here we will have a AsyncLockConnection which will tell the DB that the request will come more than one at the same time, so be ready with Locks. This file will pertain to Android projects.
- using System.IO;
- using Sample.Mobile.Services.Common;
- using SQLite.Net;
- using SQLite.Net.Async;
- using Xamarin.Forms;
- using Sample.Mobile.Droid.Helper;
- [assembly: Dependency(typeof(SqLiteAndriod))]
- namespace Sample.Mobile.Droid.Helper
- {
- public class SqLiteAndriod : ISQLite
- {
- public SqLiteAndriod()
- {
- }
- public SQLiteAsyncConnection GetAsyncConnection()
- {
- const string fileName = "BPMobile.db3";
- var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
- var path = Path.Combine(documentsPath, fileName);
- var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
- var cwLock = new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true));
- var connection = new SQLiteAsyncConnection(() => cwLock);
- return connection;
- }
- public SQLiteConnection GetConnection()
- {
- const string fileName = "BPMobile.db3";
- var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
- var path = Path.Combine(documentsPath, fileName);
- var platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();
- var connection = new SQLiteConnection(platform, path);
- return connection;
- }
- }
- }
The iOS Project SQLite File
In this we will implement the above interface functions by using dependency injection. In this we will store the DB File in APP folder itself. Here we will have a AsyncLockConnection which will tell the DB that the request will come more than one at same time, so be ready with Locks. This file will pertain to iOS projects
- using Sample.Mobile.Services.Common;
- using System;
- using SQLite.Net;
- using System.IO;
- using Xamarin.Forms;
- using Sample.Mobile.iOS.Helper;
- using SQLite.Net.Async;
- [assembly: Dependency(typeof(SqLiteiOS))]
- namespace Sample.Mobile.iOS.Helper
- {
- public class SqLiteiOS : ISQLite
- {
- public SqLiteiOS()
- {
- }
- public SQLiteAsyncConnection GetAsyncConnection()
- {
- const string fileName = "BPMobile.db3";
- var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- var path = Path.Combine(documentsPath, fileName);
- var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
- var cwLock = new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true));
- var connection = new SQLiteAsyncConnection(() => cwLock);
- return connection;
- }
- public SQLiteConnection GetConnection()
- {
- const string fileName = "BPMobile.db3";
- var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- var path = Path.Combine(documentsPath, fileName);
- var platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();
- var connection = new SQLiteConnection(platform, path);
- return connection;
- }
- }
- }
The Base Operations File
In this we’ll have a common function to insert a single entity or multiple entity. This file will be in your repository or services. In this we have two functions for inserting a single entity and multiple entity (List<entity>).

Zargam AhmadPosted Nov 24, 2021, 3:17 PM
Sir. Yeh mobile db application kaise login hogi?