From ada48d7c0e6839fc8effdfde764b424e9d81b31a Mon Sep 17 00:00:00 2001 From: huyufei Date: Tue, 30 Jul 2024 21:48:03 +0800 Subject: [PATCH] =?UTF-8?q?update:=20RemoteConfig=20=E6=8B=89=E5=8F=96?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Review?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1021111 --user=yufei.hu 【中台】【Fix】修复 RemoteConfig 接口中立即获取线上更新配置不生效的 BUG https://www.tapd.cn/33527076/s/1160717 Signed-off-by: huyufei --- .../GuruRemote/Runtime/RemoteConfigManager.cs | 171 +++++++----------- 1 file changed, 64 insertions(+), 107 deletions(-) diff --git a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs index 86f85e0..dfbbf22 100644 --- a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs +++ b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs @@ -17,8 +17,8 @@ namespace Guru /// public class RemoteConfigManager { - public const double DefaultUpdateHours = 2; - public const double DefaultFetchTimeout = 15; + private const double DefaultUpdateHours = 2; + private const double DefaultFetchTimeout = 15; internal const string Tag = "[Remote]"; private static bool _initOnce = false; private static RemoteConfigManager _instance; @@ -29,7 +29,7 @@ namespace Guru private static double _fetchIntervalHours = DefaultUpdateHours; private RemoteConfigModel _model; - internal RemoteConfigModel Model => _model ??= RemoteConfigModel.LoadOrCreate(); + private RemoteConfigModel Model => _model ??= RemoteConfigModel.LoadOrCreate(); private static Dictionary _defaultValues; @@ -87,7 +87,7 @@ namespace Guru MinimumFetchInternalInMilliseconds = (ulong)(_fetchIntervalHours * 60 * 60 * 1000) }); - _firebaseRemote.OnConfigUpdateListener += OnConfigUpdatedHandler; + _firebaseRemote.OnConfigUpdateListener += OnFirebaseConfigUpdatedHandler; // 设置默认值 AppendDefaultValues(defaults); @@ -97,143 +97,99 @@ namespace Guru _changeEvents = new Dictionary>(30); // 立即拉取所有的配置 - FetchAllConfigsImmediately(); + FetchAll(true); } private void AppendDefaultValues(Dictionary defaults) { - if (defaults != null) + if (defaults == null) return; + + if(_defaultValues == null) _defaultValues = new Dictionary(20); + + for(int i = 0; i < defaults.Keys.Count; i++) { - if(_defaultValues == null) _defaultValues = new Dictionary(20); - string key; - object value; - for(int i = 0; i < defaults.Keys.Count; i++) - { - key = defaults.Keys.ElementAt(i); - value = defaults.Values.ElementAt(i); - _defaultValues[key] = value; - } - _firebaseRemote?.SetDefaultsAsync(_defaultValues); + string key = defaults.Keys.ElementAt(i); + object value = defaults.Values.ElementAt(i); + _defaultValues[key] = value; } + _firebaseRemote?.SetDefaultsAsync(_defaultValues); } - + /// + /// 拉取所有Remote 配置并激活 (默认激活间隔 2 小时) + /// 官方文档: + /// https://firebase.google.com/docs/reference/unity/class/firebase/remote-config/firebase-remote-config#class_firebase_1_1_remote_config_1_1_firebase_remote_config_1a55b6f0ebc2b457e9c0e2ac7c52cc87fa + /// + /// 如果=true,相当于执行一次 FetchAndActivate 立即激活拉取到的配置 private void FetchAllConfigs(bool immediately = false) { var span = TimeSpan.FromHours(_fetchIntervalHours); - bool success = true; - - // if (_isDebug || immediately) - // { - // FetchAllConfigsImmediately(); - // return; - // } - + if (_isDebug || immediately) { span = TimeSpan.Zero; } _firebaseRemote.FetchAsync(span) - .ContinueWithOnMainThread(task => + .ContinueWithOnMainThread(fetchTask => { - if (task.IsFaulted || task.IsCanceled) + if (fetchTask.IsFaulted || fetchTask.IsCanceled) { - string res = task.IsFaulted? "Faulted" : "Canceled"; + string res = fetchTask.IsFaulted? "Faulted" : "Canceled"; LogE($" --- Fetch AllConfigs fails: {res}"); - success = false; + OnFetchCompleted?.Invoke(false); + return; } - if (success) + _firebaseRemote.ActivateAsync() + .ContinueWithOnMainThread(activateTask => { - _firebaseRemote.ActivateAsync() - .ContinueWithOnMainThread(task => + if (activateTask.IsFaulted || activateTask.IsCanceled) { - if (task.IsFaulted || task.IsCanceled) - { - success = false; - string res = task.IsFaulted? "Faulted" : "Canceled"; - LogE($" --- Active AllConfigs fails: {res}"); - } - else - { - success = true; - } - LogI($"[REMOTE] --- ActiveAsync success: {success}"); - OnFetchDataCompleted(); - OnFetchCompleted?.Invoke(success); - }); - } - else - { + string res = activateTask.IsFaulted? "Faulted" : "Canceled"; + LogE($" --- Active AllConfigs fails: {res}"); + OnFetchCompleted?.Invoke(false); + return; + } + + LogI($"[REMOTE] --- ActiveAsync success!"); OnFetchDataCompleted(); - OnFetchCompleted?.Invoke(success); - } + OnFetchCompleted?.Invoke(true); + }); + }); } - - /// - /// 立即拉取所有的配置 - /// - private void FetchAllConfigsImmediately() - { - bool success = true; - _firebaseRemote.FetchAndActivateAsync() - .ContinueWithOnMainThread(task => - { - if (task.IsFaulted || task.IsCanceled) - { - success = false; - string res = task.IsFaulted? "Faulted" : "Canceled"; - LogE($" --- Fetch AllConfigs fails: {res}"); - } - else - { - success = true; - LogI($"{Tag} --- FetchAndActivateAsync success"); - } - LogI($"{Tag} --- FetchAndActivateAsync success: {success}"); - OnFetchDataCompleted(); - OnFetchCompleted?.Invoke(success); - }); - } - + /// /// 获取值更新回调 /// /// /// - private void OnConfigUpdatedHandler(object sender, ConfigUpdateEventArgs updateEvent) + private void OnFirebaseConfigUpdatedHandler(object sender, ConfigUpdateEventArgs updateEvent) { if (updateEvent.Error != RemoteConfigError.None) { Debug.LogError($"{Tag} --- RemoteConfigError: {updateEvent.Error}"); + return; } - else + + _firebaseRemote.ActivateAsync().ContinueWithOnMainThread(task => { - _firebaseRemote.ActivateAsync().ContinueWithOnMainThread(task => + if (!task.IsCompleted) return; + if (updateEvent.UpdatedKeys == null) return; + + var updateKeys = updateEvent.UpdatedKeys.ToArray(); + int count = updateEvent.UpdatedKeys.Count(); + for(int i = 0; i < count; i++) { - if (task.IsCompleted) + string key = updateEvent.UpdatedKeys.ElementAt(i); + if (_changeEvents.TryGetValue(updateKeys[i], out var callback)) { - if (updateEvent.UpdatedKeys != null) - { - var updateKeys = updateEvent.UpdatedKeys.ToArray(); - string key = ""; - int i = 0; - int count = updateEvent.UpdatedKeys.Count(); - while (i < count) - { - key = updateEvent.UpdatedKeys.ElementAt(i); - if (_changeEvents.TryGetValue(updateKeys[i], out var callback)) - { - callback?.Invoke(key, _firebaseRemote.GetValue(key).StringValue); - } - i++; - } - } - } - }); - } + callback?.Invoke(key, _firebaseRemote.GetValue(key).StringValue); + } + } + }); + } @@ -268,13 +224,11 @@ namespace Guru var values = _firebaseRemote.AllValues; var updates = new Dictionary(values.Count); var configs = new Dictionary(values.Count); - ConfigValue value; - string key, str; for (int i = 0; i < values.Keys.Count; i++) { - key = values.Keys.ElementAt(i); - value = values.Values.ElementAt(i); - str = value.StringValue; + var key = values.Keys.ElementAt(i); + var value = values.Values.ElementAt(i); + var str = value.StringValue; updates[key] = str; if (IsRemoteConfigStr(str)) @@ -566,7 +520,10 @@ namespace Guru return false; } - + /// + /// 将所有云控值变化的参数通过回调通知给订阅者 + /// + /// private void DispatchUpdateValues(Dictionary updates) { Dictionary changes = new Dictionary(updates.Count);