【问题标题】:Sqlite.net async - how use in Xamarin?Sqlite.net async - 如何在 Xamarin 中使用?
【发布时间】:2015-04-19 12:49:30
【问题描述】:

我为 Android 和 Windows Phone 创建了一个应用程序。对于数据访问,我使用的是 sqllite.net 异步。我使用 PCL 库、Xamarin Android 项目和 Windows Phone 8 silverligth 项目创建了简单的示例解决方案。这是我在 PCL 中的 DataService:

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

为什么会被多次调用?调试不工作。当我在等待代码处停下来时,下一步只是退出该方法,而不会触及我的返回调用或任何东西。

这是一个简单的例子,我不明白为什么会这样。也许我做错了什么。

【问题讨论】:

  • 我认为你的东西不会被多次调用。如果我没记错的话,logcat 会为 Xamarin 的每个调试输出行显示多行。在非异步代码中放入调试语句,看看是否也重复。
  • Visual Studio 中为什么没有调试(with await)?
  • 等待的代码没有立即运行。当前方法需要在调试器将控制权交给它之前退出。如果您只在输出“Create”的行上放置一个断点,然后让它运行,您应该打断点。
  • 开始理解))。我的代码有效。我只是不明白为什么 Visual Studio 中的调试在 Windows Phone 和 Xamarin 中的工作方式不同。
  • 调试器是复杂的东西,尤其是当您将异步执行和多线程等添加到混合中时。

标签: c# sqlite windows-phone-8 asynchronous xamarin


【解决方案1】:

你的代码有问题:

button.Click += delegate
{
    TestDb();
};

TestDb 是一个异步方法,您在没有await 的情况下异步调用它。

这将在您离开Click 事件代码后调用TestDb

建议你给await打电话:

button.Click += async delegate
{
    await TestDb();
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多