如何使用 IMobileServiceSyncTable 和 Azure Mobile Services 处理分页



如何对从IMobileServiceSyncTable<T>.PullAsync()方法返回的数据进行分页?

我尝试过做这样的事情,但它没有按照我的预期工作:

myTable是一个IMobileServiceTable<T>

mySyncTable是一个IMobileServiceSyncTable<T>

// Already tracking how many records exist
// Note* Not using the sync table here, because that was not getting the correct results. 
// It seemed more correct to use the regular table so it was "live" data.
var totalCount = myTable.Take(0).IncludeTotalCount().ToListAsync();
// Try and sync a "page" of data, limited by the values here
var query = mySyncTable.Skip(count).Take(maxPerPage);
await mySyncTable.PullAsync(null, query);
// At this point, I expect the local store to have a set of data
// So I try and get that "page" of data
var results = await mySyncTable.ReadAsync(query);
// That returns 0 results, so I tried
var results = await mySyncTable.Skip(count).Take(maxPerPage).ToEnumerableAsync();
// Still 0 results, so just give me all the things
var results = await mySyncTable.ToEnumerableAsync();
// Still 0...

我得到的totalCount大于 0,但是当我认为我正在从本地商店阅读时,我似乎无法使用 Skip and Take 获得结果。请建议如何正确分页同步表中的数据。

首先,您不能在服务器和客户端之间共享查询的跳过部分(甚至不能共享),因为数据集会有所不同。

同步 10 条记录的拉取查询(跳过 10 条,取

10 条,比如拉取记录 10-20),将导致本地表有 10 条记录(假设它没有开始) 运行跳过 10,在那里取 10 条,将导致在本地找到 0 条记录。

否则,实际到页面的代码通常应该可以获取记录 X-Z。 尽管您可能希望明确排序顺序(按 X 排序),因为根据 Azure SQL 和 MySQL 的配置,默认顺序可能不一致。

现在似乎确实发生了一些问题,因为第二个 ReadAsync 也没有重新生成任何结果。 不幸的是,这可能是很多事情。

首先,我将输出 PullAsync 的结果值,它可能由于预期原因而失败(无法推送数据、与服务器的连接失败等)

如果看起来没问题,下一个最好的选择是启用像Fiddler这样的工具来查看网络上发生的事情。 客户端是否正确调用/tablename?$skip=X&Take=Y,并且服务器返回 200 并显示您期望的结果?

通常这些步骤显示最常见的原因,如果全部检查出来,下一步可能是确认可同步API写入和读取表正常(可能是表中的问题定义调用,或sql设置等本地)

以下文档指示如何进行分页: https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-how-to-use-client-library/#paging

简短版本:

// Define a filtered query that skips the top 3 items and returns the next 3 items.
MobileServiceTableQuery<TodoItem> query = todoTable
                .Skip(3)
                .Take(3);
List<TodoItem> items = await query.ToListAsync();

Azure 移动应用对返回有默认的 50 行限制。 若要重写移动应用后端中的 50 行限制,还必须将 EnableQueryAttribute 应用于公共 GET 方法并指定分页行为。应用于该方法时,以下内容将返回的最大行数设置为 1000:

[EnableQuery(MaxTop=1000)]

相关内容

最新更新