diff --git a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs index a325917..86f85e0 100644 --- a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs +++ b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs @@ -87,6 +87,8 @@ namespace Guru MinimumFetchInternalInMilliseconds = (ulong)(_fetchIntervalHours * 60 * 60 * 1000) }); + _firebaseRemote.OnConfigUpdateListener += OnConfigUpdatedHandler; + // 设置默认值 AppendDefaultValues(defaults); _initOnce = true; @@ -94,7 +96,8 @@ namespace Guru // 监听事件合集 _changeEvents = new Dictionary>(30); - FetchAllConfigs(); + // 立即拉取所有的配置 + FetchAllConfigsImmediately(); } private void AppendDefaultValues(Dictionary defaults) @@ -118,24 +121,122 @@ namespace Guru private void FetchAllConfigs(bool immediately = false) { var span = TimeSpan.FromHours(_fetchIntervalHours); - if(_isDebug || immediately) span = TimeSpan.Zero; + bool success = true; + + // if (_isDebug || immediately) + // { + // FetchAllConfigsImmediately(); + // return; + // } + + if (_isDebug || immediately) + { + span = TimeSpan.Zero; + } + _firebaseRemote.FetchAsync(span) .ContinueWithOnMainThread(task => { - bool success = true; if (task.IsFaulted || task.IsCanceled) { string res = task.IsFaulted? "Faulted" : "Canceled"; - LogE($" --- FetchAllConfigs fails: {res}"); + LogE($" --- Fetch AllConfigs fails: {res}"); success = false; } - if (success) OnFetchDataCompleted(); - - OnFetchCompleted?.Invoke(success); + if (success) + { + _firebaseRemote.ActivateAsync() + .ContinueWithOnMainThread(task => + { + 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 + { + OnFetchDataCompleted(); + OnFetchCompleted?.Invoke(success); + } }); } + /// + /// 立即拉取所有的配置 + /// + 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) + { + if (updateEvent.Error != RemoteConfigError.None) + { + Debug.LogError($"{Tag} --- RemoteConfigError: {updateEvent.Error}"); + } + else + { + _firebaseRemote.ActivateAsync().ContinueWithOnMainThread(task => + { + if (task.IsCompleted) + { + 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++; + } + } + } + }); + } + } + + #endregion