2016-05-18 24 views
29

我想要使用Firebase的新遠程配置功能獲得遠程配置參數,並且遇到問題。Firebase遠程配置:無法讀取任何值,但獲取成功

這裏是我的遠程配置控制檯:remote config console

我在做一個獲取和我的應用程序的onCreate()更新:

final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); 
remoteConfig.fetch().addOnCompleteListener(new OnCompleteListener<Void>() { 
    @Override 
    public void onComplete(@NonNull Task<Void> task) { 
     if (task.isSuccessful()) { 
      remoteConfig.activateFetched(); 
     } 
    } 
}); 

這裏就是我正在讀它:

FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); 
String value = remoteConfig.getString("active_subscriptions"); 

值返回null。

如果我打電話remoteConfig.getInfo().getLastFetchStatus(),它會返回LAST_FETCH_STATUS_SUCCESS,所以看起來取回是成功的。

任何想法爲什麼我的價值是空白?

+0

Steven,看到我的評論如下 - 我嘗試了你的修復,並沒有爲我工作,但我能夠通過移動onCreate取出工作。 – jkane001

+0

我有同樣的問題,移動取出onCreate不適用於我..如果我將它移動到onResume,它將工作在第二個簡歷,但不是最初的onResume。這並不完美,因爲我想從第一次啓動時遠程配置我的應用程序的各個方面,而不是「在稍後的某個時間點」: –

回答

7

發現問題。

添加一些日誌記錄後,我發現取回作業的onComplete()從未被調用。我將應用程序的onCreate中的抓取移至片段,現在它正常工作!

Ian Barber,這可能是一個尋找到或澄清,因爲日誌表明,火力地堡是沒有問題初始化時,它是在應用程序和取都沉默了故障。)

+0

如果您需要使用片段,該怎麼辦? – Chirag

+0

我正在使用一個片段。將'fetch()'調用移入片段爲我修復。 –

+0

這很有趣,我們會研究一下。感謝您的反饋! –

11

您很可能在遠程配置中碰到caching。它的工作方式是配置將本地緩存傳入的項目,並返回它們。因此,您最後一次(緩存)的獲取狀態可能是在定義值之前進行的,並且我們獲得了緩存的空白值。

您可以控制緩存過期,但如果您經常抓取,則可能會受到限制。

因爲這是一個共同發展的問題,但有一個開發模式,讓您請求更快地(對於小型用戶組成員):

FirebaseRemoteConfigSettings configSettings = 
    new FirebaseRemoteConfigSettings.Builder() 
     .setDeveloperModeEnabled(BuildConfig.DEBUG) 
     .build(); 
FirebaseRemoteConfig.getInstance().setConfigSettings(configSettings); 

當你調用fetch然後可以通過短緩存到期時間

long cacheExpiration = 3600; 
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); 
if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) { 
    cacheExpiration = 0; 
} 
mFirebaseRemoteConfig.fetch(cacheExpiration) 
    .addOnCompleteListener(new OnCompleteListener<Void>() { 
    // ... 
}); 

這是其在quickstart sample怎麼做,如果你想要一個完整的參考。

+6

不幸的是,這並沒有改變任何東西 –

+1

這似乎是一個不同的問題。我可以使用gFirebaseRemoteConfig.getInfo()來確認fetch從未返回,因此它不是緩存問題 –

+0

在onCreate()方法上獲取並沒有必要將它們放在onStart()中。唯一的事情你必須做的是啓用開發模式,並傳遞更少的秒數(即100)到獲取方法。它工作得很好。謝謝 – Alireza

16

解決方法發現!看到下面

我跑到「沉默完成」的事情 - 我稱之爲「抓取」,但onComplete,onSuccess或onFailure聽衆永遠不會開火。我嘗試將它移動到onCreate上,但仍然沒有發生任何事情,因此配置項永遠不會從服務器加載。我已經啓用了開發者模式,並且正在調用緩存值爲0的提取。

我能夠(一次)在「public void onComplete(@NonNull Task task){」被擊中,然後我能夠通過並且onComplete解僱了。那時我無法用其他方式重現這一結果,包括第二次做同樣的事情(我認爲)。

似乎是一個定時或併發問題,但這是沒有道理的,因爲這是一個異步調用。

解決方法

如果從活動#的onResume(或者,我想,活動#在onStart)獲取,它完美的作品。從Activity#onCreate或Application#onCreate調用獲取會導致看起來永遠無法處理的調用,事實上,在獲取開始後,應用程序的性能會明顯下降,所以我認爲有一個looper正在運行或者其他東西。*

變通辦法#2

如果你真的想這個從應用#的onCreate(我做的)運行,這似乎也工作:

new Handler().postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     // Run mFirebaseRemoteConfig.fetch(timeout) here, and it works 
    } 
}, 0); 
+0

嘗試了兩種解決方法,不起作用! – Chirag

+1

我也有這個問題,並且一個處理程序(#2)的配置獲取正常工作。很奇怪。 –

+0

此替代方法對我來說不起作用 –

2
在這種情況下

的第一件事是檢查是否有正確的firebase配置和你連接到firebase。如果你有Android Stu dio 2.2進入工具 - > Firebase-> RemoteConfig - 連接到Firebase,看看你是否收到通知說已連接。一旦連接在你的代碼中執行以下操作: mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

/** NOTE: At this point, your app can use in-app default parameter values.To use in-app 
    *  default values,skip the next section. You can deploy your app without setting 
    *  parameter values on the server,and then later set values on the server to 
    *  override the default behavior and appearance of your app. 
    */ 

    mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults); 
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() 
      .setDeveloperModeEnabled(true) 
      .build(); 
    mFirebaseRemoteConfig.setConfigSettings(configSettings); 

然後用於獲取配置做以下 長cacheExpiration = 2000; //可以增加,這通常是12小時什麼建議

/** If in developer mode cacheExpiration is set to 0 so each fetch will retrieve values from 
    * the server.*/ 

    if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) { 
     cacheExpiration = 0; 
    } 

    /** cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously 
    * fetched and cached config would be considered expired because it would have been fetched 
    * more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless 
    * throttling is in progress. The default expiration duration is 43200 (12 hours). 
    */ 

    mFirebaseRemoteConfig.fetch(cacheExpiration)//TODO Bring this from a config file 
      .addOnCompleteListener(new OnCompleteListener<Void>() { 
       @Override 
       public void onComplete(@NonNull Task<Void> task) { 
        if (task.isSuccessful()) { 
         Log.d(TAG, "Firebase Remote config Fetch Succeeded"); 
         // Once the config is successfully fetched it must be activated before newly fetched 
         // values are returned. 
         mFirebaseRemoteConfig.activateFetched(); 
        } else { 
         Log.d(TAG, "Firebase Remote config Fetch failed"); 
        } 
        showRemoteConfig(); 
       } 
      }); 

運行你的應用程序和檢查日誌「火力地堡遠程配置抓取成功」。如果你看到相同的遠程配置被加載和激活。

3

我已經使用了類似的代碼像@Ian理髮(複印件):

FirebaseRemoteConfigSettings configSettings = 
    new FirebaseRemoteConfigSettings.Builder() 
     .setDeveloperModeEnabled(BuildConfig.DEBUG) 
     .build(); 
FirebaseRemoteConfig.getInstance().setConfigSettings(configSettings); 

我的問題是「BuildConfig.DEBUG」,則返回false。所以它在緩存中取值1h直到它被再次提取!

3

我有同樣的問題,沒有解決方法在我的情況下是有幫助的。問題出在測試設備上。我使用模擬器時未安裝Google Mobile Services,因此Complete事件未被解僱。我用GMS試了我的手機,一切都很順利。祝你好運。