update: RemoteConfig 拉取修复 Review
--story=1021111 --user=yufei.hu 【中台】【Fix】修复 RemoteConfig 接口中立即获取线上更新配置不生效的 BUG https://www.tapd.cn/33527076/s/1160717 Signed-off-by: huyufei <yufei.hu@castbox.fm>
							parent
							
								
									cd83fd8bab
								
							
						
					
					
						commit
						ada48d7c0e
					
				|  | @ -17,8 +17,8 @@ namespace Guru | |||
|     /// </summary> | ||||
|     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<string, object> _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<string, Action<string,string>>(30); | ||||
|              | ||||
|             // 立即拉取所有的配置             | ||||
|             FetchAllConfigsImmediately(); | ||||
|             FetchAll(true); | ||||
|         } | ||||
| 
 | ||||
|         private void AppendDefaultValues(Dictionary<string, object> defaults) | ||||
|         { | ||||
|             if (defaults != null) | ||||
|             if (defaults == null) return; | ||||
|              | ||||
|             if(_defaultValues == null) _defaultValues = new Dictionary<string, object>(20); | ||||
| 
 | ||||
|             for(int i = 0; i < defaults.Keys.Count; i++) | ||||
|             { | ||||
|                 if(_defaultValues == null) _defaultValues = new Dictionary<string, object>(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); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 拉取所有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 | ||||
|         /// </summary> | ||||
|         /// <param name="immediately">如果=true,相当于执行一次 FetchAndActivate 立即激活拉取到的配置</param> | ||||
|         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);    | ||||
|                     }); | ||||
|                  | ||||
|                 }); | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 立即拉取所有的配置 | ||||
|         /// </summary> | ||||
|         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);   | ||||
|                 }); | ||||
|         } | ||||
| 
 | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 获取值更新回调 | ||||
|         /// </summary> | ||||
|         /// <param name="sender"></param> | ||||
|         /// <param name="updateEvent"></param> | ||||
|         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<string, string>(values.Count); | ||||
|             var configs = new Dictionary<string, string>(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; | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 将所有云控值变化的参数通过回调通知给订阅者 | ||||
|         /// </summary> | ||||
|         /// <param name="updates"></param> | ||||
|         private void DispatchUpdateValues(Dictionary<string, string> updates) | ||||
|         { | ||||
|             Dictionary<string, string> changes = new Dictionary<string, string>(updates.Count); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue