【问题标题】:Xamarin Offline Sync issue with getting data获取数据的 Xamarin 离线同步问题
【发布时间】:2019-08-13 20:02:50
【问题描述】:

我在使用 android 模拟器从 azure 移动后端 api 返回数据时遇到了一些问题。移动应用程序是用 Xamarin 编写的,我使用的是 MobileServiceClient 和 IMobileServiceSyncTable。以下是我编写的代码:

var _mobileServiceClient = new MobileServiceClient(url);
var store = new MobileServiceSQLiteStore("notesdb.db");
store.DefineTable<Notes>();
_mobileServiceClient.SyncContext.InitializeAsync(store);
var _notesTable = _mobileServiceClient.GetSyncTable<Notes>();

var temp = await _notesTable.ReadAsync();

后端代码如下:

public IQueryable<Notes> GetAllNotes()
{
    return Query(); 
}

每当我这样做时,应用程序都会变得无响应并且永远不会返回。它就像处于死锁模式。

有人遇到过这个问题吗?

【问题讨论】:

标签: sqlite xamarin.forms offline


【解决方案1】:

看了MobileServiceClient的用法后:https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-dotnet-how-to-use-client-library

你的电话似乎很好,除了一个:

_mobileServiceClient.SyncContext.InitializeAsync(store);

由于您不等待此方法,因此不会为您的下一个方法初始化同步上下文。

所以只要等待方法,你应该没问题:

await _mobileServiceClient.SyncContext.InitializeAsync(store);

您几乎每次都可以应用一个通用规则:始终等待返回 Task 对象的方法。

此外,由于您处于服务/存储库层,您应该 ConfigureAwait(false) 您的方法:

var _mobileServiceClient = new MobileServiceClient(url);
var store = new MobileServiceSQLiteStore("notesdb.db");
store.DefineTable<Notes>();
await _mobileServiceClient.SyncContext.InitializeAsync(store).ConfigureAwait(false);
var _notesTable = _mobileServiceClient.GetSyncTable<Notes>();

var temp = await _notesTable.ReadAsync().ConfigureAwait(false);

这样做您的代码将不会在 UI 线程中运行(虽然不能保证,但我不想让您感到困惑 :)。由于您没有在同一个线程上运行代码,因此也会减少可能的死锁。

更多信息:https://medium.com/bynder-tech/c-why-you-should-use-configureawait-false-in-your-library-code-d7837dce3d7f

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2011-09-10
    相关资源
    最近更新 更多