/// <summary> /// Process results from the loaders /// Call MUST be made under a lock /// </summary> private void ProcessResults() { List <LoadResult> orderedResults; switch (_cacheEntry.CachePolicy) { case CachePolicy.CacheThenRefresh: { orderedResults = _loadResults.Where(result => result.Loader == LoaderType.CacheLoader).ToList(); _loadResults = _loadResults.Where(result => result.Loader != LoaderType.CacheLoader).ToList(); if (!_activeLoaders.Contains(LoaderType.CacheLoader)) { //There is no cache loader active - we can return live results orderedResults.AddRange(_loadResults); _loadResults = new List <LoadResult>(); } } break; case CachePolicy.NoCache: case CachePolicy.ValidCacheOnly: case CachePolicy.AutoRefresh: case CachePolicy.Forever: //oder doesn't metter orderedResults = _loadResults; _loadResults = new List <LoadResult>(); break; default: throw new ArgumentOutOfRangeException(); } if (orderedResults != null && orderedResults.Count() > 0) { bool errorHandlerCalled = false; List <CompletionHandler> subscribers = new List <CompletionHandler>(_subscribers); foreach (var loadResult in orderedResults) { if (loadResult.Error == null) { foreach (var subscriber in subscribers.Where(subscriber => subscriber.SuccessAction != null)) { CompletionHandler localSubscriber = subscriber; PriorityQueue.AddUiWorkItem(() => localSubscriber.SuccessAction(), false); } } else { foreach (var subscriber in subscribers.Where(subscriber => subscriber.ErrorAction != null)) { var localSubscriber = subscriber; LoadResult localResult = loadResult; PriorityQueue.AddUiWorkItem(() => localSubscriber.ErrorAction(localResult.Error), false); errorHandlerCalled = true; } } // if the error isn't handled, throw. if (loadResult.Error != null && !errorHandlerCalled) { #if DEBUG var st = _cacheEntry.LastLoadStackTrace; _cacheEntry.LastLoadStackTrace = null; Debug.WriteLine("{4}: FAIL loading {0} (ID={1}). Exception {2} Message={3}", _cacheEntry.ObjectType.Name, _cacheEntry.LoadContext.Identity, loadResult.Error.GetType().Name, loadResult.Error.Message, DateTime.Now); if (st != null) { Debug.WriteLine("Load initiated from:\r\n" + st); } #endif if (UnhandledError != null) { UnhandledError(loadResult.Error); } } } if (_activeLoaders.Count == 0) { _subscribers = _subscribers.Except(subscribers).ToList(); } } }