This question is locked. New answers and comments are not allowed.
I have a bit specific scenario: i don't know size of the page before parsing server's answer. Also, i want to keep everything flexible enough (no special code for first call).
So, my function is looking like this (viewmodel):
One of the problems that i faced was that first VirtualDataCollectionOnItemsLoading() is called at the page construction time. But i'm getting actual SelectedIndex (just product category index) only in the OnNavigatedTo().
Solution is quite simple: no subscription by default, unsubscribe while navigatingFrom, subscribe while navigatingTo the page again.
Now i'm getting another problem: _dataService.ReadFromCacheAsync() is called 2 times in a row, just one after another. And that produces quite notable delay, i see empty page for about 2 seconds.
First callstack is quite okay: http://clip2net.com/s/5AYleg , but the second one is quite weird: http://clip2net.com/s/5AYjqc
The most mysterious part for me - is that this appears only when i'm starting to read from cache. If i'm doing internet request, no second calls.
Maybe i'm wrong at some point, so here is just raw information: when i'm loading page for the first time (no cache), everything is smooth and nice: i see animation while waiting for server answer, page is responsible, etc.But when i'm visiting the page second time, i expect to see cached results instantly. Instead, i see a non-responsible blank page for about 2 seconds (shit, its even longer than waiting for servers answer).
Any ideas?
So, my function is looking like this (viewmodel):
public
MyViewModel(IProductSubSubCategoriesDataService dataService)
{
_dataService = dataService;
SelectedIndex = String.Empty;
VirtualDataCollection =
new
VirtualizingDataCollection(1, Constants.PAGE_SIZE_PRODUCT_SUB_SUB_CATEGORY);
// 30 for now
}
}
private
void
VirtualDataCollectionOnItemsLoading(
object
sender, VirtualizingDataCollectionItemsLoadingEventArgs e)
{
if
(String.IsNullOrEmpty(SelectedIndex))
return
;
ThreadPool.QueueUserWorkItem(async o =>
{
var answerCached = await _dataService.ReadFromCacheAsync(SelectedIndex, e.StartIndex, e.Count);
if
(answerCached.Status == AnswerDataServiceStatus.Ok)
{
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
lock
(locker)
{
VirtualDataCollection.ItemsLoading -= VirtualDataCollectionOnItemsLoading;
VirtualDataCollection.Count = answerCached.TotalItems;
VirtualDataCollection.LoadItems(e.StartIndex, answerCached.Collection);
VirtualDataCollection.ItemsLoading += VirtualDataCollectionOnItemsLoading;
IsBusy =
false
;
Status = String.Empty;
}
});
}
else
{
var answer = await _dataService.RequestServerAsync(SelectedIndex, e.StartIndex, e.Count);
if
(answer.Status == AnswerDataServiceStatus.Ok)
// && answer.Collection.SequenceEqual(VirtualDataCollection.))
{
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
lock
(locker)
{
VirtualDataCollection.ItemsLoading -= VirtualDataCollectionOnItemsLoading;
VirtualDataCollection.Count = answer.TotalItems;
VirtualDataCollection.LoadItems(e.StartIndex, answer.Collection);
VirtualDataCollection.ItemsLoading += VirtualDataCollectionOnItemsLoading;
IsBusy =
false
;
Status = String.Empty;
}
});
}
else
{
if
(String.Equals(Status, MainResources.Loading_process))
DispatcherHelper.CheckBeginInvokeOnUI(() => Status = MainResources.No_network_available);
}
}
});
}
public
void
OnNavigatedTo()
{
//ItemsList = null;
Status = MainResources.Loading_process;
VirtualDataCollection.ItemsLoading += VirtualDataCollectionOnItemsLoading;
VirtualDataCollection.Count = 1;
}
public
void
OnNavigatedFrom()
{
SelectedIndex = String.Empty;
VirtualDataCollection.ItemsLoading -= VirtualDataCollectionOnItemsLoading;
VirtualDataCollection.Clear();
Cleanup();
}
One of the problems that i faced was that first VirtualDataCollectionOnItemsLoading() is called at the page construction time. But i'm getting actual SelectedIndex (just product category index) only in the OnNavigatedTo().
Solution is quite simple: no subscription by default, unsubscribe while navigatingFrom, subscribe while navigatingTo the page again.
Now i'm getting another problem: _dataService.ReadFromCacheAsync() is called 2 times in a row, just one after another. And that produces quite notable delay, i see empty page for about 2 seconds.
First callstack is quite okay: http://clip2net.com/s/5AYleg , but the second one is quite weird: http://clip2net.com/s/5AYjqc
The most mysterious part for me - is that this appears only when i'm starting to read from cache. If i'm doing internet request, no second calls.
Maybe i'm wrong at some point, so here is just raw information: when i'm loading page for the first time (no cache), everything is smooth and nice: i see animation while waiting for server answer, page is responsible, etc.But when i'm visiting the page second time, i expect to see cached results instantly. Instead, i see a non-responsible blank page for about 2 seconds (shit, its even longer than waiting for servers answer).
Any ideas?