我有一个Xamarin.Forms应用程序,它使用Azure App Service和SQLLite Offline Sync。 调用SyncContext.InitializeAsync时,会出现死锁,函数InitializeAsync从未完成。
在此主题中找到了解决方案:Azure/Xamarin Mobile App Hangs at SyncContext.InitializeAsync
这有效:
System.Threading.Tasks.Task.Run(() => this.IDataProvider.IMobileServiceClient.SyncContext.InitializeAsync(localStore, _syncHandler)).Wait();
这不是:
await this.IDataProvider.IMobileServiceClient.SyncContext.InitializeAsync(localStore, _syncHandler);
整个功能:
public override async System.Threading.Tasks.Task Init()
{
string storePath = System.IO.Path.Combine(Microsoft.WindowsAzure.MobileServices.MobileServiceClient.DefaultDatabasePath, localStoreName);
Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore localStore = new Microsoft.WindowsAzure.MobileServices.SQLiteStore.MobileServiceSQLiteStore(storePath);
localStore.DefineTable<CPM.Recruitment.Mobile.Freelancer.DataObjects.Entities.Promoter>();
System.Threading.Tasks.Task.Run(() => this.IDataProvider.IMobileServiceClient.SyncContext.InitializeAsync(localStore, _syncHandler)).Wait();
//await this.IDataProvider.IMobileServiceClient.SyncContext.InitializeAsync(localStore, _syncHandler);
_promoters = new Azure.AppService.DataObjects.Client.Sync.List<CPM.Recruitment.Mobile.Freelancer.DataObjects.Entities.Promoter>(this, this.IDataProvider.IMobileServiceClient.GetSyncTable<CPM.Recruitment.Mobile.Freelancer.DataObjects.Entities.Promoter>());
}
但为什么呢?我不想使用Wait();
答案 0 :(得分:0)
await和Wait方法之间的区别是:
await意味着当前任务将以异步方式在分离的线程中执行,并且不会阻止调用线程。如果调用线程是UI线程。 UI线程将继续运行其他操作。
等待方法是同步方法。它使调用线程等到当前任务完成。如果调用线程是UI线程,则UI操作将被阻止。
如果您使用await来运行不会阻止UI线程的任务,似乎会导致死锁。