Skip to content

Commit 488b7d1

Browse files
committed
Fixed concurrency issue. Added ClearCache(url)
1 parent 0301812 commit 488b7d1

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

Assets/_PackageRoot/Runtime/ImageLoader.LoadSprite.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,19 @@ static async void InternalLoadSprite(Future<Sprite> future, Vector2 pivot, Textu
132132

133133
request = UnityWebRequestTexture.GetTexture(future.Url);
134134
request.timeout = (int)Math.Ceiling(settings.timeout.TotalSeconds);
135-
var asyncOperation = request.SendWebRequest();
136-
await asyncOperation.WithCancellation(future.CancellationToken);
135+
future = future.Canceled(request.Abort);
136+
await request.SendWebRequest();
137137
}
138138
catch (OperationCanceledException)
139139
{
140140
future.Cancel();
141141
}
142+
catch (TimeoutException e)
143+
{
144+
RemoveLoading(future); // LOADING REMOVED
145+
future.FailToLoad(e);
146+
return;
147+
}
142148
catch (Exception e)
143149
{
144150
if (settings.debugLevel <= DebugLevel.Exception && !ignoreImageNotFoundError)

Assets/_PackageRoot/Runtime/ImageLoader.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
using UnityEngine;
2+
using System;
23
using System.Collections.Generic;
34
using System.Threading.Tasks;
5+
using System.Collections.Concurrent;
6+
using System.Linq;
47

58
namespace Extensions.Unity.ImageLoader
69
{
710
public static partial class ImageLoader
811
{
9-
private static Dictionary<string, Future<Sprite>> loadingInProcess = new Dictionary<string, Future<Sprite>>();
12+
private static ConcurrentDictionary<string, Future<Sprite>> loadingInProcess = new ConcurrentDictionary<string, Future<Sprite>>();
1013
private static void AddLoading(Future<Sprite> future)
1114
{
12-
loadingInProcess.Add(future.Url, future);
15+
if (!loadingInProcess.TryAdd(future.Url, future))
16+
throw new Exception($"[ImageLoader] AddLoading: {future.Url} already loading");
17+
1318
if (settings.debugLevel <= DebugLevel.Log)
1419
Debug.Log($"[ImageLoader] AddLoading: {future.Url}, total {loadingInProcess.Count} loading tasks");
1520
}
16-
private static void RemoveLoading(Future<Sprite> future)
21+
private static void RemoveLoading(Future<Sprite> future) => RemoveLoading(future.Url);
22+
private static void RemoveLoading(string url)
1723
{
18-
if (loadingInProcess.Remove(future.Url))
24+
if (loadingInProcess.TryRemove(url, out var future))
1925
{
2026
if (settings.debugLevel <= DebugLevel.Log)
21-
Debug.Log($"[ImageLoader] RemoveLoading: {future.Url}, left {loadingInProcess.Count} loading tasks");
27+
Debug.Log($"[ImageLoader] RemoveLoading: {url}, left {loadingInProcess.Count} loading tasks");
28+
}
29+
else
30+
{
31+
if (settings.debugLevel <= DebugLevel.Warning)
32+
Debug.LogWarning($"[ImageLoader] RemoveLoading: {url} not found in loading tasks");
2233
}
2334
}
2435

@@ -48,17 +59,29 @@ public static void Init()
4859
/// Return all current loading Futures
4960
/// </summary>
5061
/// <returns>Returns read only list of all current loading Futures</returns>
51-
public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values;
62+
public static IReadOnlyCollection<Future<Sprite>> GetLoadingFutures() => loadingInProcess.Values.ToArray();
5263

5364
/// <summary>
5465
/// Clear cache from Memory and Disk layers for all urls
5566
/// </summary>
67+
/// <returns>Returns task of the disk cache clearing process</returns>
5668
public static Task ClearCache()
5769
{
5870
ClearMemoryCache();
5971
return ClearDiskCache();
6072
}
6173

74+
/// <summary>
75+
/// Clear cache from Memory and Disk layers for all urls
76+
/// </summary>
77+
/// <param name="url">URL to the picture, web or local</param>
78+
/// <returns>Returns task of the disk cache clearing process</returns>
79+
public static Task ClearCache(string url)
80+
{
81+
ClearMemoryCache(url);
82+
return ClearDiskCache(url);
83+
}
84+
6285
/// <summary>
6386
/// Checks cache at Memory and at Disk by the url
6487
/// </summary>

Assets/_PackageRoot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Ivan Murzak",
66
"url": "https://github.com/IvanMurzak"
77
},
8-
"version": "5.1.3",
8+
"version": "5.2.0",
99
"unity": "2019.2",
1010
"description": "Asynchronous image loading from remote or local destination. It has two layers of configurable Memory and Disk cache systems.",
1111
"dependencies": {

0 commit comments

Comments
 (0)