gpt4 book ai didi

xamarin.forms - xamarin 表单 azure 移动应用程序同步缓慢

转载 作者:行者123 更新时间:2023-12-03 14:57:39 25 4
gpt4 key购买 nike

我正在使用带有 Xamarin.Forms 的 Azure 移动应用程序来创建支持脱机的移动应用程序。

我的解决方案基于 https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/client/

这是我用于离线同步的代码:

public class AzureDataSource
{
private async Task InitializeAsync()
{
// Short circuit - local database is already initialized
if (client.SyncContext.IsInitialized)
{
return;
}

// Define the database schema
store.DefineTable<ArrayElement>();
store.DefineTable<InputAnswer>();
//Same thing with 16 others table
...

// Actually create the store and update the schema
await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
}

public async Task SyncOfflineCacheAsync()
{
await InitializeAsync();

//Check if authenticated
if (client.CurrentUser != null)
{
// Push the Operations Queue to the mobile backend
await client.SyncContext.PushAsync();

// Pull each sync table
var arrayTable = await GetTableAsync<ArrayElement>();
await arrayTable.PullAsync();

var inputAnswerInstanceTable = await GetTableAsync<InputAnswer>();
await inputAnswerInstanceTable.PullAsync();

//Same thing with 16 others table
...
}
}

public async Task<IGenericTable<T>> GetTableAsync<T>() where T : TableData
{
await InitializeAsync();
return new AzureCloudTable<T>(client);
}
}
public class AzureCloudTable<T>
{
public AzureCloudTable(MobileServiceClient client)
{
this.client = client;
this.table = client.GetSyncTable<T>();
}

public async Task PullAsync()
{
//Query name used for incremental pull
string queryName = $"incsync_{typeof(T).Name}";

await table.PullAsync(queryName, table.CreateQuery());
}
}

问题是即使没有任何东西要拉(Android 设备上 8-9 秒,拉整个数据库超过 25 秒),同步也需要很长时间。

我看了 Fiddler找出移动应用程序后端需要多少时间来响应,每个请求大约需要 50 毫秒,所以问题似乎不是来自这里。

有没有人有同样的烦恼?有什么我做错的地方或提高同步性能的提示吗?

最佳答案

我们的特殊问题与我们的数据库迁移有关。数据库中的每一行都有相同的 updatedAt值(value)。我们运行了一个 SQL 脚本来修改它们,使它们都是唯一的。

此修复实际上是针对我们遇到的其他一些问题,其中并非所有行都因某种未知原因返回,但我们也看到了显着的速度改进。

此外,另一个改善加载时间的奇怪修复如下。

在我们第一次拉取所有数据后(可以理解,这需要一些时间) - 我们做了一个 UpdateAsync()在返回的行之一上,之后我们没有推送它。

我们已经了解到离线同步的工作方式是它会拉取任何有日期的东西 较新 比最近更新的。与此相关的速度略有提高。

最后,我们为提高速度所做的最后一件事是不再获取数据,如果它已经在 View 中缓存了一个副本。不过,这可能不适用于您的用例。

public List<Foo> fooList = new List<Foo>

public void DisplayAllFoo()
{
if(fooList.Count == 0)
fooList = await SyncClass.GetAllFoo();

foreach(var foo in fooList)
{
Console.WriteLine(foo.bar);
}
}

2019 年 3 月 20 日编辑:
有了这些改进,我们仍然看到非常缓慢的同步操作,使用方式与 OP 中提到的方式相同,还包括我在此处的回答中列出的改进。

我鼓励所有人就如何提高这种速度分享他们的解决方案或想法。

关于xamarin.forms - xamarin 表单 azure 移动应用程序同步缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com