firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
826 stars 428 forks source link

Firestore string value is empty #895

Closed SebastienDevQuebec closed 3 years ago

SebastienDevQuebec commented 3 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

Not sure if it can be reproduced.

Here is a link to the detailed issue on StackOverflow: https://stackoverflow.com/questions/65174003/unity-firestore-weird-bug#comment115384693_65174003

In summary, I listen to a document, and with the value I do things. it works great in the editor but when compiled on my mobile the value is always = to "" (nothing).

SebastienDevQuebec commented 3 years ago

Anyone?

wilhuff commented 3 years ago

Sorry for the trouble with this. I'm currently working on reproducing your findings and will update when I have an idea of what's going wrong.

SebastienDevQuebec commented 3 years ago

Ok thank you, I am gonna try downgrading the Firebase plugin

SebastienDevQuebec commented 3 years ago

Sorry for the trouble with this. I'm currently working on reproducing your findings and will update when I have an idea of what's going wrong.

In case you want to know, I just tested it... and it works perfectly in 6.16.1

wilhuff commented 3 years ago

That's good to know. I'm still working on reproducing this in a way that I can debug what's going on.

wilhuff commented 3 years ago

I've been unable to reproduce the empty value you're describing.

All the values I read and write come through without issue. I've tried several variations based on your stackoverflow post:

In all cases I'm unable to observe the empty value that you're seeing in your logs.

I've been reproducing using an emulated Pixel 2 running Android 7.0. Do you observe this error when running on an Android emulator? If you don't see this on the emulator but do observe it on a physical device, which device is it?

Another tack to try is to look to see if this is somehow specific to your application. Do you see the same kind of empty values if you add logging to the Firestore Unity quickstart?

SebastienDevQuebec commented 3 years ago

My phone is a Samsung S9+ Don't have time to test it on an emulator

I have reproduced the empty value on a new project:

here is a screenshot in yellow I fill up manually, the rest is a "Cloud Function" function... Capture screenshot of the result: I restart my app 10x and the manual data is still there and perfect. let me remind you that the other data the one generated by the cloud function is ok the first time the app open, but after the restart value is null. Screenshot_20201215-222317_ThinkFast

I am stopping here it is late I have to work tomorrow at 5h30 it's almost 23h00 hiiiii need my 8hours.

I think the Cloud function has something to do whit the problem.

UI_TchatManager.cs

using Firebase.Firestore;
using System.Collections.Generic;
using UnityEngine;

public class UI_TchatManager : MonoBehaviour
{
    // Default Instance
    public static UI_TchatManager Default = null;

    private ListenerRegistration SocialDocumentListener = null;

    private void Awake()
    {
        // Set Instance
        if (Default == null)
        {
            Default = this;
        }
        else
        {
            // Destroy Class
            Destroy(this.gameObject);
            return;
        }

        DontDestroyOnLoad(this);

        Debug.Log("<b>Tchat:</b> Awake");
    }

    // Get Tchat Message
    public void AttachListener(int iLobbyID)
    {
        if (FirebaseDependencies.Default.bIsFirebaseAvailable)
        {
            FirebaseFirestore db = FirebaseFirestore.DefaultInstance;

            DocumentReference SocialDocument = db.Document($"Social/Lobby 1");

            // Attach Listener
            SocialDocumentListener = null;

            SocialDocumentListener = SocialDocument.Listen(snapshot => {
                Debug.Log("Attached!");
                // Snapshot
                Dictionary<string, object> TchatDictionary = snapshot.ToDictionary();

                foreach (KeyValuePair<string, object> x in TchatDictionary)
                {
                    Debug.Log($"<color=purple>Social/Lobby 1 -> Key:{x.Key} / Value:{x.Value} ({x.Value.ToString().Length})</color>");
                }
            });
        }
        else
        {
            Debug.LogWarning($"Firebase is not available");
        }
    }
}

FirebaseDependencies.cs

using Firebase;
using Firebase.Firestore;
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FirebaseDependencies : MonoBehaviour
{
    // Set Default Instance
    public static FirebaseDependencies Default = null;

    public bool bIsFirebaseAvailable = false;

    private void Awake()
    {
        // Set Instance
        if (Default == null)
        {
            Default = this;
        }
        else
        {
            Destroy(this.gameObject);
            return;
        }
        DontDestroyOnLoad(this);

        Debug.Log("<b>Firebase Dependencies:</b> Awake");

        StartCoroutine("CheckFirebaseDependencies");
    }

    // Check Status
    private IEnumerator CheckFirebaseDependencies()
    {
        var TaskCheckDependencies = FirebaseApp.CheckAndFixDependenciesAsync();

        yield return new WaitUntil(() => TaskCheckDependencies.IsCompleted);

        if (TaskCheckDependencies.Result == Firebase.DependencyStatus.Available)
        {
            Debug.Log($"<b>Firebase Dependencies:</b> {TaskCheckDependencies.Result}");
            bIsFirebaseAvailable = true;
        }
        else
        {
            Debug.LogWarning(TaskCheckDependencies.Result);
            bIsFirebaseAvailable = false;
        }
    }

    // Check Error
    private bool CheckError(AggregateException exception, int firebaseExceptionCode)
    {
        Firebase.FirebaseException fbEx = null;
        foreach (Exception e in exception.Flatten().InnerExceptions)
        {
            fbEx = e as Firebase.FirebaseException;
            if (fbEx != null)
                break;
        }

        if (fbEx != null)
        {
            if (fbEx.ErrorCode == firebaseExceptionCode)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return false;
    }
}
wilhuff commented 3 years ago

I've followed your steps (though I had to change the code because nothing was actually calling AttachListener) but I can see this working via a local emulator and also on a Samsung Galaxy S9+ (G965F) and also see the values coming through:

12-16 10:41:35.600: I/Unity(25687): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
12-16 10:41:35.969: I/Unity(25687): Attached!
12-16 10:41:35.969: I/Unity(25687):  
12-16 10:41:35.969: I/Unity(25687): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
12-16 10:41:36.055: I/Unity(25687): <color=purple>Social/Lobby 1 -> Key:bar / Value:42 (2)</color>
12-16 10:41:36.055: I/Unity(25687):  
12-16 10:41:36.055: I/Unity(25687): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
12-16 10:41:36.055: I/Unity(25687): <color=purple>Social/Lobby 1 -> Key:foo / Value:hello world (11)</color>

I've also written a small node.js program that writes to Socal/Lobby 1 and I can observe the values it writes from your app running on Android.

If you can put together an example that reproduces this issue I'll happily look into it further, but at this point I suspect there must be a bug somewhere in your application, possibly in the function that's writing values to this document.

SebastienDevQuebec commented 3 years ago

Anyway, I just gonna let that go.... everything works fine in firebase 6.16.1!

I am 100% sure it is not my app.

  1. on Firestore 7.0.1 it doesn't work -> but on Firestore 6.16.1 it works.
  2. and everything works on the editor when it is packaged on android it bug.
  3. There a new posts on this forum that look like my problem https://github.com/firebase/quickstart-unity/issues/900

Thank you, and see you next bug. bye bye

wilhuff commented 3 years ago

I'm genuinely interested in finding and fixing this issue, but can't do anything without being able to observe the issue myself.

You mentioned that adding data via the console works, but the values don't show when added via a function. Is it possible there are differences in the kind of data you're adding in these cases? For example, is the function setting more or slightly different data? Could you send the complete contents of a document that's not working?

SebastienDevQuebec commented 3 years ago

I only have one document in social called Lobby 1 all the data I add manually works but all the cloud function generated data does not work (psst: only on android maybe ios too did not test ios) on the unity engine everything works perfectly.

_M1DQOIZEPSebTHEking:"ANDCA1213181337211000Bein oui" Generated by cloud function will not work. but the same _M1DQOIZEPSebTHEking:"ANDCA1213181337211000Bein oui" enter manually in Firestore will work

Capture1

async function InitSendMessageV2(data, context)
{
  return new Promise(resolve => {
    setTimeout(async () => {
      // Firebase Reference
      const database = admin.firestore();
      // Date Stamp
      var date = new Date();
      var dateStr = 
      ("00" + (date.getMonth() + 1)).slice(-2) +
      ("00" + date.getDate()).slice(-2) +
      ("00" + date.getHours()).slice(-2) +
      ("00" + date.getMinutes()).slice(-2) +
      ("00" + date.getSeconds()).slice(-2);
      // 
      var LoginRow = {}; 
      LoginRow["M_" + IdGenerator(8) + data.Name] = data.Platform + data.CountryCode + dateStr + (("000000" + (data.Color * 1000)).slice(-6)) + data.Message;
      // Get Lobby Document
      const GetLobbyDocument = await database.collection('Social').doc("Lobby " + data.LobbyID).set((LoginRow), {merge: true});
      resolve(true);
    });
  });
}

Just to recap: 1- Happen only when build on Android. 2- The Key is ok but the value is empty "" only when it's a string... the numbers are working correctly. 3- Data generated by Cloud functions does not work but data added manually works 4- My app has a collection call Leaderboard and data are created by cloud functions too but everything works perfectly and value are string too. see screen shoot below 5- Firestore 6.16.1 work but 7.0.1 does not

Capture2

wilhuff commented 3 years ago

Is it possible that the strings that are failing contain unicode characters? If that's the case, this is the same issue as #900.

SebastienDevQuebec commented 3 years ago

You can see for yourself in my last post, all failed except 2 of them that I manually enter

SebastienDevQuebec commented 3 years ago

To reproduce the bug make sure to use the -> Package Manager -> Firestore 7.0.1

wilhuff commented 3 years ago

I'm definitely using Firestore 7.0.1.

I've adapted the cloud function code you provided to write data that looks very similar to what you've shown:

{
  "M_K95dMZB6yhhB58xS3GBASebTHEking": "ANDCA1216222233211000Saliut",
  "M_iZW425FCYi5eqXNhUze7SebTHEking": "ANDCA1216222233211000salut",
  "M_f8XXjaMGvIgFIOVastO9SebTHEking": "ANDCA1216222233211000hrhf",
  "M_zMp94xX5Wn0tzX4SLctFSebTHEking": "ANDCA1216222233211000gdf",
  "M_5D6jmNULKB3LUKh2ax5jPlayer39463": "ANDCA1216222232029000you taking to you",
  "M_ht2UtsxU8IusBXxbx7FUSebTHEking": "ANDCA1216222233211000Salut",
  "M_XJkTPVlbwAeASol5ZTxZSebTHEking": "ANDCA1216222233211000lok",
  "M_5SBZ5BFRSiouZlcdx9LiSebTHEking": "ANDCA1216222233211000lol",
  "M_OcWYWFwwUcZfw9nMF3YuPlayer39463": "ANDCA1216222233029000lala",
  "M_sztPY7ZRMBMREXwfuzdXSebTHEking": "ANDCA1216222232211000loo",
  "M_0w2z4AmcjR95QyXhUl5RSebTHEking": "ANDCA1216222232211000Bein oui"
}

Using this data the values still come through.

Even after bumping the number of entries in the document to 100 the values still come through.

If I add a non-ASCII character, this does cause the value to end up empty--this is the effect of issue #900. If this were happening to you you'd see a StringIndexOutOfBoundsException in the logs. However, #900 only has its effect if the string contains non-ASCII characters. Do you see any exceptions in logs (with adb logcat)?

tamotamago commented 3 years ago

I've been facing the same issue. It happens on only Android devices. It works fine on iPhone and Unity editor.

wilhuff commented 3 years ago

@tamotamago Firestore in release 7.0.1 contained a bug in the way it handled Unicode strings that resulted in empty string values (#900). We've released 7.0.2 which fixes that. It's not clear if this issue is the same.

I've been unable to reproduce the issue described here--string values being empty only when changed via Cloud Function--so it's not clear if these are the same issue. Are you seeing StringIndexOutOfBoundsException exceptions in the Android log?

SebastienDevQuebec commented 3 years ago

I can't see the 7.0.2 on unity Package manager yet... I will test it as soon as it comes out.

tamotamago commented 3 years ago

@wilhuff 7.0.2 works well for me. Thanks!

I've been unable to reproduce the issue described here--string values being empty only when changed via Cloud Function--so it's not clear if these are the same issue. Are you seeing StringIndexOutOfBoundsException exceptions in the Android log?

In my case, Japanese and German characters were emptied. English characters are fetched correctly. And I did not see any exceptions on Android Logcat.

schmidt-sebastian commented 3 years ago

@tamotamago Thank you for confirming that 7.0.2 fixed your issue. I will close this issue based on this assessment.

schmidt-sebastian commented 3 years ago

@SebastienDevQuebec - We will re-open this issue if you are still blocked.