async Task <Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); Stream stream = null; if (CachingEnabled) { stream = await GetStreamFromCacheAsync(uri, cancellationToken).ConfigureAwait(false); } if (stream == null) { try { stream = await Device.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { Xamarin.Forms.Internals.Log.Warning("Image Loading", $"Error getting stream for {Uri}: {ex}"); stream = null; } } return(stream); }
async Task <Stream> GetStreamAsyncUnchecked(string key, Uri uri, CancellationToken cancellationToken) { if (await GetHasLocallyCachedCopyAsync(key).ConfigureAwait(false)) { var retry = 5; while (retry >= 0) { int backoff; try { Stream result = await Store.OpenFileAsync(Path.Combine(CacheName, key), FileMode.Open, FileAccess.Read).ConfigureAwait(false); return(result); } catch (IOException) { // iOS seems to not like 2 readers opening the file at the exact same time, back off for random amount of time backoff = new Random().Next(1, 5); retry--; } if (backoff > 0) { await Task.Delay(backoff); } } return(null); } Stream stream; try { stream = await Device.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false); if (stream == null) { return(null); } } catch (Exception ex) { Log.Warning("Image Loading", $"Error getting stream for {Uri}: {ex}"); return(null); } Stream writeStream = await Store.OpenFileAsync(Path.Combine(CacheName, key), FileMode.Create, FileAccess.Write).ConfigureAwait(false); await stream.CopyToAsync(writeStream, 16384, cancellationToken).ConfigureAwait(false); if (writeStream != null) { writeStream.Dispose(); } stream.Dispose(); return(await Store.OpenFileAsync(Path.Combine(CacheName, key), FileMode.Open, FileAccess.Read).ConfigureAwait(false)); }
async Task <Stream> GetStreamAsync(Uri uri, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); Stream stream; if (!CachingEnabled) { try { stream = await Device.GetStreamAsync(uri, cancellationToken).ConfigureAwait(false); } catch (Exception) { stream = null; } } else { stream = await GetStreamFromCacheAsync(uri, cancellationToken).ConfigureAwait(false); } return(stream); }