Skip to content

Commit 856f7d7

Browse files
Anton PetrovNevazhnovu
authored andcommitted
fix race conditions by dispatching JSON parsing to the UnityMain thread
(cherry picked from commit 534e9bc)
1 parent 87f107e commit 856f7d7

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

Assets/ConsentManagementProvider/Scripts/observer/BroadcastEventDispatcher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ public static void Execute<T>(BaseEventData eventData, ExecuteEvents.EventFuncti
1919
actions.Enqueue(delegate { ExecuteEvents.Execute<T>(handler, eventData, functor); });
2020
}
2121
}
22+
23+
public static void Execute(Action action)
24+
{
25+
actions.Enqueue(action);
26+
}
2227
}
2328
}

Assets/ConsentManagementProvider/Scripts/wrapper/Android/SpClientProxy.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using ConsentManagementProviderLib.Json;
3+
using ConsentManagementProviderLib.Observer;
34
using UnityEngine;
45

56
namespace ConsentManagementProviderLib.Android
@@ -58,16 +59,20 @@ AndroidJavaObject onAction(AndroidJavaObject view, AndroidJavaObject actionType)
5859
void onConsentReady(string spConsents)
5960
{
6061
CmpDebugUtil.Log("I've reached the C# onConsentReady with json string: " + spConsents);
61-
try
62-
{
63-
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
64-
_spConsents = consents;
65-
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
66-
}
67-
catch (Exception e)
62+
63+
BroadcastEventDispatcher.Execute(() =>
6864
{
69-
ConsentMessenger.Broadcast<IOnConsentError>(e);
70-
}
65+
try
66+
{
67+
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
68+
_spConsents = consents;
69+
ConsentMessenger.Broadcast<IOnConsentReady>(consents);
70+
}
71+
catch (Exception e)
72+
{
73+
ConsentMessenger.Broadcast<IOnConsentError>(e);
74+
}
75+
});
7176
}
7277

7378
/**
@@ -78,16 +83,20 @@ void onSpFinished(string spConsents)
7883
CmpDebugUtil.ForceEnableNextCmpLog();
7984
CmpDebugUtil.LogWarning($"I've reached the C# onSpFinished with JSON spConsents={spConsents}");
8085
Console.WriteLine($"spConsents= `{spConsents}");
81-
try
82-
{
83-
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
84-
_spConsents = consents;
85-
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
86-
}
87-
catch (Exception e)
86+
87+
BroadcastEventDispatcher.Execute(() =>
8888
{
89-
ConsentMessenger.Broadcast<IOnConsentError>(e);
90-
}
89+
try
90+
{
91+
SpConsents consents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents);
92+
_spConsents = consents;
93+
ConsentMessenger.Broadcast<IOnConsentSpFinished>(consents);
94+
}
95+
catch (Exception e)
96+
{
97+
ConsentMessenger.Broadcast<IOnConsentError>(e);
98+
}
99+
});
91100
}
92101

93102
void onError(AndroidJavaObject rawThrowableObject)

0 commit comments

Comments
 (0)