Sqlite.net async - 在 Xamarin 中如何使用?
Sqlite.net async - how use in Xamarin?
我为 Android 和 Windows Phone 创建了一个应用程序。对于数据访问,我使用 sqllite.net 异步。我使用 PCL 库、Xamarin Android 项目和 Windows Phone 8 silverligth 项目创建了简单的示例解决方案。这是我在 PCL:
中的数据服务
public class DataService
{
private SQLiteAsyncConnection _dbConnection;
public DataService(ISQLitePlatform platform, string path)
{
var connectionFactory = new Func<SQLiteConnectionWithLock>
(() => new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true)));
_dbConnection = new SQLiteAsyncConnection(connectionFactory);
}
public async Task Initialize()
{
await _dbConnection.CreateTableAsync<ToDo>().ContinueWith(t =>
{
Debug.WriteLine("Create");
});
}
public async Task<int> AddNewToDo(ToDo item)
{
var result = await _dbConnection.InsertAsync(item);
return result;
}
public async Task<List<ToDo>> GetAllToDos()
{
var result = await _dbConnection.Table<ToDo>().OrderByDescending(t => t.TimeStamp).ToListAsync();
return result;
}
....
}
这是在Windows中使用Phone:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var db = new DataService(new SQLitePlatformWP8(), "my.db");
await db.Initialize();
await db.AddNewToDo(new ToDo {Text = "Hello world"});
var items = await db.GetAllToDos();
Debug.WriteLine("Count - {0}",items.Count);
}
在Windows中输出Phone:
Create
Count - 1
没关系。调试正常。
这是在 Xamarin 中使用 Android:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
TestDb();
};
}
private async void TestDb()
{
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, "my.db");
var db = new DataService(new SQLitePlatformAndroid(), path);
await db.Initialize();
await db.AddNewToDo(new ToDo { Text = "Hello world" });
var items = await db.GetAllToDos();
Console.WriteLine("count - {0}",items.Count);
}
输出:
[0:]
Create
[0:] Create
02-18 00:46:01.167 I/mono-stdout(19234): Create
count - 1
02-18 00:46:01.675 I/mono-stdout(19234): count - 1
为什么调用不止一次?调试不工作。当我用 await 停在代码处时,下一步只是退出方法,而不会触及我的 return 调用或任何东西。
这是一个简单的例子,我不明白为什么会这样。也许我做错了什么。
您的代码有问题:
button.Click += delegate
{
TestDb();
};
TestDb
是一种异步方法,您在没有 await
的情况下异步调用它。
这将在您离开 Click
事件代码后调用 TestDb
。
我建议你await
来电:
button.Click += async delegate
{
await TestDb();
};
我为 Android 和 Windows Phone 创建了一个应用程序。对于数据访问,我使用 sqllite.net 异步。我使用 PCL 库、Xamarin Android 项目和 Windows Phone 8 silverligth 项目创建了简单的示例解决方案。这是我在 PCL:
中的数据服务public class DataService
{
private SQLiteAsyncConnection _dbConnection;
public DataService(ISQLitePlatform platform, string path)
{
var connectionFactory = new Func<SQLiteConnectionWithLock>
(() => new SQLiteConnectionWithLock(platform, new SQLiteConnectionString(path, true)));
_dbConnection = new SQLiteAsyncConnection(connectionFactory);
}
public async Task Initialize()
{
await _dbConnection.CreateTableAsync<ToDo>().ContinueWith(t =>
{
Debug.WriteLine("Create");
});
}
public async Task<int> AddNewToDo(ToDo item)
{
var result = await _dbConnection.InsertAsync(item);
return result;
}
public async Task<List<ToDo>> GetAllToDos()
{
var result = await _dbConnection.Table<ToDo>().OrderByDescending(t => t.TimeStamp).ToListAsync();
return result;
}
....
}
这是在Windows中使用Phone:
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var db = new DataService(new SQLitePlatformWP8(), "my.db");
await db.Initialize();
await db.AddNewToDo(new ToDo {Text = "Hello world"});
var items = await db.GetAllToDos();
Debug.WriteLine("Count - {0}",items.Count);
}
在Windows中输出Phone:
Create
Count - 1
没关系。调试正常。
这是在 Xamarin 中使用 Android:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate
{
TestDb();
};
}
private async void TestDb()
{
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, "my.db");
var db = new DataService(new SQLitePlatformAndroid(), path);
await db.Initialize();
await db.AddNewToDo(new ToDo { Text = "Hello world" });
var items = await db.GetAllToDos();
Console.WriteLine("count - {0}",items.Count);
}
输出:
[0:]
Create
[0:] Create
02-18 00:46:01.167 I/mono-stdout(19234): Create
count - 1
02-18 00:46:01.675 I/mono-stdout(19234): count - 1
为什么调用不止一次?调试不工作。当我用 await 停在代码处时,下一步只是退出方法,而不会触及我的 return 调用或任何东西。
这是一个简单的例子,我不明白为什么会这样。也许我做错了什么。
您的代码有问题:
button.Click += delegate
{
TestDb();
};
TestDb
是一种异步方法,您在没有 await
的情况下异步调用它。
这将在您离开 Click
事件代码后调用 TestDb
。
我建议你await
来电:
button.Click += async delegate
{
await TestDb();
};