15
15
*/
16
16
17
17
using OptimizelySDK . Logger ;
18
+ using System . Collections . Concurrent ;
18
19
using System . Collections . Generic ;
19
20
20
21
namespace OptimizelySDK . Notifications
@@ -23,8 +24,7 @@ internal static class NotificationCenterRegistry
23
24
{
24
25
private static readonly object _mutex = new object ( ) ;
25
26
26
- private static Dictionary < string , NotificationCenter > _notificationCenters =
27
- new Dictionary < string , NotificationCenter > ( ) ;
27
+ private static ConcurrentDictionary < string , NotificationCenter > _notificationCenters = new ConcurrentDictionary < string , NotificationCenter > ( ) ;
28
28
29
29
/// <summary>
30
30
/// Thread-safe access to the NotificationCenter
@@ -40,18 +40,16 @@ public static NotificationCenter GetNotificationCenter(string sdkKey, ILogger lo
40
40
return default ;
41
41
}
42
42
43
- NotificationCenter notificationCenter ;
44
- lock ( _mutex )
43
+ NotificationCenter notificationCenter = null ;
44
+
45
+ if ( _notificationCenters . ContainsKey ( sdkKey ) )
45
46
{
46
- if ( _notificationCenters . ContainsKey ( sdkKey ) )
47
- {
48
- notificationCenter = _notificationCenters [ sdkKey ] ;
49
- }
50
- else
51
- {
52
- notificationCenter = new NotificationCenter ( logger ) ;
53
- _notificationCenters [ sdkKey ] = notificationCenter ;
54
- }
47
+ notificationCenter = _notificationCenters [ sdkKey ] ;
48
+ }
49
+ else
50
+ {
51
+ notificationCenter = new NotificationCenter ( logger ) ;
52
+ _notificationCenters [ sdkKey ] = notificationCenter ;
55
53
}
56
54
57
55
return notificationCenter ;
@@ -74,7 +72,7 @@ public static void RemoveNotificationCenter(string sdkKey)
74
72
out NotificationCenter notificationCenter ) )
75
73
{
76
74
notificationCenter . ClearAllNotifications ( ) ;
77
- _notificationCenters . Remove ( sdkKey ) ;
75
+ _notificationCenters . TryRemove ( sdkKey , out NotificationCenter obj ) ;
78
76
}
79
77
}
80
78
}
0 commit comments