示例#1
0
        internal async Task <PendingGetResult> DownloadItems(IList <ItemKey> keys, IList <string> typeVersions, PendingGetCompletionDelegate callback, CancellationToken cancelToken)
        {
            var result = new PendingGetResult();

            try
            {
                result.KeysRequested = keys;

                ItemQuery query = this.CreateRefreshQueryForKeys(keys, typeVersions);

                IList <RecordItem> items = await m_remoteStore.GetAllItemsAsync(query);

                await this.PersistDownloadedItemsAsync(items);

                result.KeysFound = (
                    from item in items
                    select item.Key
                    ).ToArray();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            NotifyPendingGetComplete(callback, result);

            return(result);
        }
        private async Task BeginRefreshAsync(int startAt, bool shouldAwait, CancellationToken cancelToken)
        {
            IList <ItemKey> keysToDownload = m_data.Keys.CollectKeysNeedingDownload(startAt, ReadAheadChunkSize);

            if (keysToDownload.IsNullOrEmpty())
            {
                return;
            }
            //
            // Refresh happens in the background
            // This will return as soon as the task is launched
            //
            PendingGetCompletionDelegate completionCallback = null;

            if (!shouldAwait)
            {
                completionCallback = PendingGetCompletion; // Callback => download items in background
            }

            PendingGetResult result = await Store.RefreshAsyncImpl(
                keysToDownload,
                m_data.TypeVersions,
                completionCallback,
                cancelToken);

            if (result == null)
            {
                return; // NO pending work
            }

            PendingGetCompletion(this, result);
        }
        private void ProcessNotFoundItems(PendingGetResult pendingResult)
        {
            if (pendingResult.KeysNotFound.IsNullOrEmpty() || !m_data.HasKeys)
            {
                return;
            }

            m_data.Keys.SetLoadingStateForKeys(pendingResult.KeysNotFound, false);
            ItemsNotFound.SafeInvokeInUIThread(this, pendingResult.KeysNotFound);
        }
 private void ProcessError(PendingGetResult pendingResult, Exception ex)
 {
     try
     {
         m_data.Keys.SetLoadingStateForKeys(pendingResult.KeysRequested, false);
         Error.SafeInvokeInUIThread(this, ex);
     }
     catch
     {
     }
 }
示例#5
0
 private void NotifyPendingGetComplete(PendingGetCompletionDelegate callback, PendingGetResult result)
 {
     if (callback != null)
     {
         try
         {
             callback(this, result);
         }
         catch
         {
         }
     }
 }
        private void ProcessFoundItems(PendingGetResult pendingResult, bool fireEvent)
        {
            if (pendingResult.KeysFound.IsNullOrEmpty() || !m_data.HasKeys)
            {
                return;
            }

            m_data.Keys.SetLoadingStateForKeys(pendingResult.KeysFound, false);

            if (fireEvent)
            {
                ItemsAvailable.SafeInvokeInUIThread(this, pendingResult.KeysFound);
            }
        }
        private void PendingGetCompletion(object sender, PendingGetResult result)
        {
            try
            {
                result.EnsureSuccess();

                ProcessFoundItems(result, true);

                ProcessNotFoundItems(result);
            }
            catch (Exception ex)
            {
                ProcessError(result, ex);
            }
        }
示例#8
0
        internal async Task <PendingGetResult> DownloadAsyncImpl(IList <ItemKey> keys, IList <string> typeVersions, PendingGetCompletionDelegate callback, CancellationToken cancelToken)
        {
            if (callback != null)
            {
                //
                // Run the download in the background.
                // Return what we have right away, and notify caller when pending items arrive
                //
                Task task = DownloadItems(keys, typeVersions, callback, cancelToken);
                return(null);
            }

            //
            // Wait for download to complete...
            //
            PendingGetResult result = await DownloadItems(keys, typeVersions, callback, cancelToken);

            result.EnsureSuccess();

            return(result);
        }
示例#9
0
        internal async Task <IList <RecordItem> > GetItemsAsyncImpl(IList <ItemKey> keys, IList <string> typeVersions,
                                                                    PendingGetCompletionDelegate callback,
                                                                    CancellationToken cancelToken)
        {
            //
            // true: include null items - i.e. items not found in the local store
            //
            IList <RecordItem> foundItems = await m_localStore.GetItemsAsyncImpl(keys, true);

            //
            // Trigger a download of items that are not available yet...
            //
            IList <ItemKey> pendingKeys = CollectKeysNeedingDownload(keys, typeVersions, foundItems);

            if (pendingKeys.IsNullOrEmpty())
            {
                return(foundItems);
            }

            PendingGetResult pendingResult = await DownloadAsyncImpl(pendingKeys, typeVersions, callback, cancelToken);

            if (pendingResult == null)
            {
                return(foundItems);
            }

            //
            // Load fresh items
            //
            if (pendingResult.HasKeysFound)
            {
                await LoadNewItems(foundItems, keys, pendingResult.KeysFound);
            }

            return(foundItems);
        }
示例#10
0
 private void ProcessError(PendingGetResult pendingResult, Exception ex)
 {
     try
     {
         SetLoadingStateForKeys(pendingResult.KeysRequested, false);
         Error.SafeInvoke(this, ex);
     }
     catch
     {
     }
 }
示例#11
0
        private void ProcessNotFoundItems(PendingGetResult pendingResult)
        {
            if (pendingResult.KeysNotFound.IsNullOrEmpty() || !m_data.HasKeys)
            {
                return;
            }

            SetLoadingStateForKeys(pendingResult.KeysNotFound, false);
            ItemsNotFound.SafeInvoke(this, pendingResult.KeysNotFound);
        }
示例#12
0
        private void ProcessFoundItems(PendingGetResult pendingResult, bool fireEvent)
        {
            if (pendingResult.KeysFound.IsNullOrEmpty() || !m_data.HasKeys)
            {
                return;
            }

            SetLoadingStateForKeys(pendingResult.KeysFound, false);

            if (fireEvent)
            {
                ItemsAvailable.SafeInvoke(this, pendingResult.KeysFound);
            }
        }
示例#13
0
        private void PendingGetCompletion(object sender, PendingGetResult result)
        {
            try
            {
                result.EnsureSuccess();

                ProcessFoundItems(result, true);

                ProcessNotFoundItems(result);
            }
            catch (Exception ex)
            {
                ProcessError(result, ex);
            }
        }
示例#14
0
 private void NotifyPendingGetComplete(PendingGetCompletionDelegate callback, PendingGetResult result)
 {
     if (callback != null)
     {
         try
         {
             callback(this, result);
         }
         catch
         {
         }
     }
 }
示例#15
0
        private async Task<PendingGetResult> DownloadItems(
            IList<ItemKey> keys, 
            IList<string> typeVersions,
            PendingGetCompletionDelegate callback, 
            CancellationToken cancelToken)
        {
            var result = new PendingGetResult();
            try
            {
                result.KeysRequested = keys;

                ItemQuery query = ItemQuery.QueryForKeys(keys);
                query.View.SetSections(SectionsToFetch);
                if (typeVersions != null && typeVersions.Count > 0)
                {
                    query.View.TypeVersions.AddRange(typeVersions);
                }

                IList<RecordItem> items = await m_record.GetAllItemsAsync(query);
                await m_itemStore.PutItemsAsyncImpl(items);

                result.KeysFound = (
                    from item in items
                    select item.Key
                    ).ToArray();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            NotifyPendingGetComplete(callback, result);

            return result;
        }
        internal async Task<PendingGetResult> DownloadItems(IList<ItemKey> keys, IList<string> typeVersions, PendingGetCompletionDelegate callback, CancellationToken cancelToken)
        {
            var result = new PendingGetResult();
            try
            {
                result.KeysRequested = keys;

                ItemQuery query = this.CreateRefreshQueryForKeys(keys, typeVersions);

                IList<RecordItem> items = await m_remoteStore.GetAllItemsAsync(query);
                
                 await this.PersistDownloadedItemsAsync(items);

                result.KeysFound = (
                    from item in items
                    select item.Key
                    ).ToArray();
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }

            NotifyPendingGetComplete(callback, result);

            return result;
        }