2016-03-08 71 views
3

如何爲從IMobileServiceSyncTable<T>.PullAsync()方法返回的數據分頁?如何處理使用IMobileServiceSyncTable和Azure移動服務的分頁

我試着做這樣的事情,但它不工作我沒有料到:

myTableIMobileServiceTable<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,但是當我認爲我正在從本地商店閱讀時,我似乎無法通過跳過並取得結果。請建議如何正確分頁來自同步表的數據。

回答

1

以下文件顯示如何做分頁: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行的限制。要覆蓋Mobile App後端的50行限制,還必須將EnableQueryAttribute應用於公共GET方法並指定分頁行爲。當施加到該方法中,以下將最大返回的行至1000:

[EnableQuery(MaxTop=1000)] 
+0

感謝您的回覆。我找到了這些文檔,這是我獲得基本實施的地方。但是,這並不是專門針對「SyncTable」,這是我的困惑即將到來的地方。如果我不使用SyncTable,而只是使用IMobileServiceTable,它將按預期工作。問題是使用同步表,並分頁同步/本地讀取。 – therealjohn

2

開始時,你不能共享一個查詢的跳過部分(或者甚至採取)服務器之間&客戶端,因爲數據集會有所不同。

同步10個記錄(跳過10,取10,拉取記錄10-20)的拉查詢將導致本地表有10條記錄(假設它沒有啓動)運行跳過10,取10在那裏,會導致在本地找到0個記錄。

否則,頁面的實際代碼應該一般用於獲取記錄X-Z。雖然您可能希望在排序順序上排序(order by X),因爲取決於Azure SQL的配置,所以默認順序可能不會對齊。

現在確實出現了一些問題,因爲第二個ReadAsync也沒有迴應結果。不幸的是,它可能是一些事情。

首先我會輸出PullAsync的結果值,也可能是失敗的原因,預計(數據不能推,到服務器的連接失敗等)

如果這看起來不錯,下一個最好的打賭是使像Fiddler這樣的工具能夠看到電線上發生了什麼。客戶端是否正確地調用/ tablename?$ skip = X & Take = Y,並且服務器返回200,結果如您所期望的那樣?

通常這些步驟顯示最常見的原因,如果全部檢出,下一步可能會確認可同步的API寫入&對錶的讀取很好(可能是表中的問題定義調用或SQL設置,本地等)

+0

你在第一句話中說得很好。我將不得不仔細檢查這在我的情況下不是問題。我在兩種情況下都以0-10開始,雖然這很容易引起您提到的問題,但我的方案與索引不同步不匹配。 我確實有相當數量的日誌記錄/錯誤處理,特別是在我的SyncHandler中,這似乎不是問題。我會進一步調查你的其他建議並回報。謝謝你的時間。 – therealjohn

+0

爲了避免浪費,我會在這裏獎勵賞金,因爲這個答案涉及同步表,並提供一些關於如何進一步調查的建議 - 希望能夠找到我期待的答案。 – therealjohn