Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Firebase Database: Save/Retrieve Offline Data

Discussion in 'Assets and Asset Store' started by EchtMe, Mar 28, 2018.

  1. EchtMe

    EchtMe

    Joined:
    Jan 16, 2018
    Posts:
    10
    Hi there,

    I want to use Firebase to update my content (text) on my mobile app.

    What is working so far
    I can see direct changes on the mobile, from the the Firebase Database, when there is an internet connection at least at the start of the app.

    Problem
    When the app starts offline, there are no "offline" data available.

    Idea
    Is there a way to save the Firebase as JSON locally and let this file auto update if there is an internet connection? I want to keep it very simple and just fetch new data, if there is a connection and changes - otherwise use local/current data.

    Thanks and Cheers
     
    rakkarage likes this.
  2. EchtMe

    EchtMe

    Joined:
    Jan 16, 2018
    Posts:
    10
    Ok, I found a solution:

    Code (CSharp):
    1.        
    2. FirebaseDatabase.DefaultInstance
    3.             .RootReference
    4.             .GetValueAsync ()
    5.             .ContinueWith (task => {
    6.             DataSnapshot snapshot = task.Result;
    7.                string JSONfile = saveToJSON(snapshot.GetRawJsonValue());
    8. });
     
    rakkarage likes this.
  3. daverinnadesan

    daverinnadesan

    Joined:
    Mar 13, 2018
    Posts:
    4
    Hi @EchtMe

    How were you able to catch the fault of the task. I am finding it difficult to detect when the task fails if offline.
     
  4. vfxjex

    vfxjex

    Joined:
    Jun 3, 2013
    Posts:
    92
    I manage to solve this by adding this code during the start of your app.
    Code (CSharp):
    1. FirebaseDatabase.DefaultInstance.SetPersistenceEnabled(true);
    It works fine in iOS and in Editor, however I have problem working it in Android.
     
unityunity