Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Remote config Cache is working only the first time while disconnected

Discussion in 'Unity Remote Config' started by FranArjona, Jan 15, 2022.

  1. FranArjona


    Oct 25, 2015
    Hello everyone,

    I have setup a basic workflow in which as we could expect:
    1. I set data first from Default values
    2. Then a handler tries to retrieve data from Remote
    3. If no connection, Cache acts
    4. If none of them, then Default data appears
    It was working well until I saw an odd case and I cannot find anything particularly wrong in the code.

    This is the flow I follow and this part is working well so far:
    • Create a variable in remote config and code (default). Assign a different value for both
    • Load it once with the Internet ON and it works => It gets the last remote value
    • Disconnect the Internet and stop the app
    • Pay and check the variable and confirmed => It gets the last remote config value (cached)
    • Stop the editor/mobile and play again, still with the Internet OFF
    • The value is still retrieved from the last remote value (2 connections ago)
    • The value has been reset to the default value. In fact, I can check and the key and value don't exist from the 2nd connection onwards (using ConfigManager.appConfig.HasKey())

    So the flow works well in general: default values, remote values and cache. But from the second consecutive session that I try to get data while disconnected, I can see the cache has been lost, as in cache only works based on the very last session.

    I wonder if this is by design for some reason (please explain why in that case) or just a bug. In the second case, I can report it.

    I don't think the code is needed, but I'll leave it as a reference or to confirm that :
    Code (CSharp):
    1.         void Awake()
    2.         {
    3.             // Singleton before
    4.             //...
    6.             AssignAppRemoteConfigDefaults();
    8.             ConfigManager.FetchCompleted += ApplyRemoteSettingsHandler;
    10.             ConfigManager.SetEnvironmentID(Settings.RemoteConfigEnvironmentId);
    12.             ConfigManager.SetCustomUserID(AnalyticsSessionInfo.userId);
    14.             ConfigManager.FetchConfigs<UserAttributes, AppAttributes>(new UserAttributes(), new appAttributes());
    15.         }
    17.         private static void AssignAppRemoteConfigDefaults()
    18.         {
    19.             AppRemoteConfig.MyStaticVariable = AppRemoteConfig.DefaultMyStaticVariable;
    20.         }
    22.         // Apply remote settings handler
    23.         private void ApplyRemoteSettingsHandler(ConfigResponse configResponse)
    24.         {
    25.             // Conditionally update settings, depending on the response's origin:
    26.             switch (configResponse.requestOrigin) {
    27.                 case ConfigOrigin.Default:
    28.                     break;
    29.                 // BTW, I have seen another potential bug here in which ConfigOrigin.Cached redirects to ConfigOrigin.Remote. It doesn't make a difference, though
    30.                 case ConfigOrigin.Cached:
    31.                 case ConfigOrigin.Remote:
    32.                     // I have also tried to remove Defaults from here as in the docs, but the same problem
    33.                     AppRemoteConfig.MyStaticVariable = ConfigManager.appConfig.GetInt("myStaticVariable", AppRemoteConfig.DefaultMyStaticVariable);
    34.                     assignmentId = ConfigManager.appConfig.assignmentId;
    35.                     break;
    36.             }
    37.         }
  2. JeffDUnity3D


    May 2, 2017
    @FranArjona You have no code for the Cached and Default switch conditions. Please share the code you are using for your test.
  3. FranArjona


    Oct 25, 2015
    Sure, my code is following the given example, so that's the reason Default and Cached don't have code:

    Code (CSharp):
    2.        switch (configResponse.requestOrigin) {
    3.            case ConfigOrigin.Default:
    4.                Debug.Log ("No settings loaded this session; using default values.");
    5.                break;
    6.            case ConfigOrigin.Cached:
    7.                Debug.Log ("No settings loaded this session; using cached values from a previous session.");
    8.                break;
    9.            case ConfigOrigin.Remote:
    10.                // AppRemoteConfig.MyStaticVariable and AppRemoteConfig.DefaultMyStaticVariable are just static integers from another file
    11.                AppRemoteConfig.MyStaticVariable = ConfigManager.appConfig.GetInt("myStaticVariable", AppRemoteConfig.DefaultMyStaticVariable);
    12.                assignmentId = ConfigManager.appConfig.assignmentId;
    13.                break;
    14.        }
    And on top of that I already checked the forum to avoid asking the same and you mention to do that:

    I'll clarify the use cases again. Every use case is tested by stopping the application, connecting/disconnecting the Internet physically and Play in the editor again:
    - Normal testing connected to the Internet. It retrieves REMOTE config. WORKING
    - Disconnecting the Internet. It retrieves the previous REMOTE data. WORKING
    - Keep the Internet disconnected and repeating again. It retrieves the DEFAULT config instead of the LAST REMOTE data. NOT WORKING or not working as I would expect (correct me if I'm wrong)

    Again, the basic use cases are working, I just think it cache doesn't work after the second attempt, by a bug or by design. (I have the feeling the cache file is removed after being read once)
  4. JeffDUnity3D


    May 2, 2017
    Sorry I don't see where you are debugging your scenarios. How are you debugging? Where are you seeing the last remote data? I would recommend Debug.Log the actual values you are receiving. You are only outputting a static string. Also, ensure to be using RC 3.0.0-pre.9
  5. FranArjona


    Oct 25, 2015
    Yes, I was debugging using Debug.Log, I just removed all the debugging code from the snippet.

    I was using it directly on the source value. I also checked the key to see if I was doing something wrong:

    And the results were:
    - Internet ON=> Right value (from remote), the key existed
    - Internet OFF (first attempt) => Right value (from cache), the key existed
    - Internet OFF (second consecutive attempt) => Wrong value (0), the key didn't exist

    I tried this several times to confirm it was a consistent result.
  6. JeffDUnity3D


    May 2, 2017
    Please share the actual code that I would use to attempt to reproduce your issue.
  7. FranArjona


    Oct 25, 2015
    Thank you, I appreciate the support.

    I have removed extra comments/features to simplify it for your test. I have also retested it right now to confirm it keeps not working/as expected. Hopefully you are able to reproduce it (notice the only thing I cannot provide is the Settings.RemoteConfigEnvironmentId as it is a personal credential. I can share it via MP if you need to)

    Notice the default value is 60 and the Remote is 70.

    The test I just did again to confirm it displayed in Debug.Log:
    - Internet ON. Play in Editor => True, 70 (WORKING)
    - Stop. Internet OFF. Play => True, 70 (WORKING)
    - Stop. Internet remains OFF. Play => False, 0 (NOT WORKING)

    Code (csharp):
    1. using Config;
    2. using Unity.RemoteConfig;
    3. using UnityEngine;
    4. using UnityEngine.Analytics;
    6. namespace Controllers.Global
    7. {
    8.     public class GlobalRemoteConfigProvider : MonoBehaviour
    9.     {
    10.         private static int _interstitialAdsFrequency;
    11.         private static readonly int DefaultInterstitialAdsFrequency = 60;
    13.         private struct UserAttributes {
    14.         }
    15.         private struct AppAttributes {
    16.             public string appVersion;
    17.         }
    18.         public string assignmentId;
    20.         void Awake()
    21.         {
    22.             // Defaults
    23.             _interstitialAdsFrequency = DefaultInterstitialAdsFrequency;
    24.             // Remote config
    25.             ConfigManager.FetchCompleted += ApplyRemoteSettingsHandler;
    27.             ConfigManager.SetEnvironmentID(Settings.RemoteConfigEnvironmentId);
    28.             ConfigManager.SetCustomUserID(AnalyticsSessionInfo.userId);
    29.             ConfigManager.FetchConfigs<UserAttributes, AppAttributes>(new UserAttributes(), new AppAttributes());
    30.         }
    32.         private void ApplyRemoteSettingsHandler(ConfigResponse configResponse)
    33.         {
    34.             switch (configResponse.requestOrigin) {
    35.                 case ConfigOrigin.Default:
    36.                     Debug.Log ("No settings loaded this session; using default values.");
    37.                     break;
    38.                 case ConfigOrigin.Cached:
    39.                     Debug.Log ("No settings loaded this session; using cached values from a previous session.");
    40.                     break;
    41.                 case ConfigOrigin.Remote:
    42.                     Debug.Log(ConfigManager.appConfig.HasKey("interstitialAdsFrequency").ToString());
    43.                     Debug.Log(ConfigManager.appConfig.GetInt("interstitialAdsFrequency").ToString());
    44.                     _interstitialAdsFrequency = ConfigManager.appConfig.GetInt("interstitialAdsFrequency", DefaultInterstitialAdsFrequency);
    45.                     assignmentId = ConfigManager.appConfig.assignmentId;
    46.                     break;
    47.             }
    48.         }
    49.     }
    50. }
    Last edited: Jan 18, 2022
  8. JeffDUnity3D


    May 2, 2017
    @FranArjona Again, you are not retrieving and writing out the values in the Default or Cached switch case, only Remote.
  9. FranArjona


    Oct 25, 2015
    Yes, I already replied to that, because I'm following what is explained here:
    And also what you mention here (cache should work automatically):

    But the key is understanding that those switch cases are even NOT ACCESSED in my tests anyway. You can see there is a log line and it's never printed out. I cannot see these messages at all in the whole flow, so it doesn't matter what I put in there:
    Debug.Log("No settings loaded this session; using default values.");
    Debug.Log("No settings loaded this session; using cached values from a previous session.");

    I can see only what I posted above. I know people come from different backgrounds and I could be mistaken or not understanding the expected behaviour, but I'd like you to try if you can, please (Unity 2021.2.8f1, btw), because this looks like a bug.

    I hope I'm explaining myself well and you are able to have the same findings. Thanks.
  10. JeffDUnity3D


    May 2, 2017
    If there is no internet and the user has not connected previously, the Default case would be executed. If there is no internet and the user has connected previously, the Cached case would be executed. This is done "automatically". And ensure to be using 3.0.0-pre.9.
  11. FranArjona


    Oct 25, 2015
    Ok, in case this is helpful for anyone
    Ok, thank you. I'm using the latest stable version 2.1.2, but, sure, I'll give it a try to the 3.0.0 pre-release version.