如何处理IMobileServiceSyncTable和Azure移动服务的分页

时间:2016-03-08 01:43:02

标签: c# pagination azure-mobile-services

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

我已经尝试过这样的事情,但是我的预期并没有发挥作用:

myTableIMobileServiceTable<T>

mySyncTableIMobileServiceSyncTable<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获得结果。请建议如何从同步表中正确分页数据。

2 个答案:

答案 0 :(得分:3)

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

同步10条记录的拉取查询(跳过10,取10,说拉10-20),将导致本地表有10条记录(假设无法启动)运行跳过10,在那里取10 ,将导致在本地找到0条记录。

否则,对于获取记录X-Z,页面的实际代码通常是正常的。虽然您可能希望明确排序顺序(orderby X),因为依赖于Azure SQL&amp;的配置。 MySQL默认订单可能不对齐。

现在确实出现了一些问题,因为第二个ReadAsync也没有结果。不幸的是,这可能是很多事情。

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

如果看起来不错,那么下一个最好的办法就是让像Fiddler这样的工具能够看到线路上发生了什么。客户端是否正确调用/ tablename?$ skip = X&amp; Take = Y,服务器返回200,结果是您期望的?

通常这些步骤显示最常见的原因,如果全部检出,下一步可能是确认可同步的API写入&amp;对表格读得很好(可能是表格定义调用或本地sql设置等问题)

答案 1 :(得分:1)

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

简短版本:

skip_name_resolve

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

// 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();