How to store an array to Firebase via Unity SDK












0















I am trying to store an Int32 Array and bool Array to Firebase but it isn't working for me. I have searched on various places, but couldn't find a solution. Can anyone tell me, how one can store an array to Firebase Real-time DB from Unity.



I am using System.Reflection to get all the public static fields of the class UserPrefs.



Here is the code, I am trying to do this job...



        System.Type type = typeof(UserPrefs);
FieldInfo fields = type.GetFields();
foreach (var field in fields) {
if (user != null)
dbReference.Child("users").Child(user.UserId).Child(field.Name).SetValueAsync(field.GetValue(null));
else
Debug.LogError("There is no user LoggedIn to write...");
}


Above code saves all values other than arrays. Error given on arrays is following:




InvalidCastException: Cannot cast from source type to destination
type.
Firebase.Database.Internal.Utilities.Validation.ValidateWritableObject
(System.Object object)




Any help will be much appreciated...










share|improve this question



























    0















    I am trying to store an Int32 Array and bool Array to Firebase but it isn't working for me. I have searched on various places, but couldn't find a solution. Can anyone tell me, how one can store an array to Firebase Real-time DB from Unity.



    I am using System.Reflection to get all the public static fields of the class UserPrefs.



    Here is the code, I am trying to do this job...



            System.Type type = typeof(UserPrefs);
    FieldInfo fields = type.GetFields();
    foreach (var field in fields) {
    if (user != null)
    dbReference.Child("users").Child(user.UserId).Child(field.Name).SetValueAsync(field.GetValue(null));
    else
    Debug.LogError("There is no user LoggedIn to write...");
    }


    Above code saves all values other than arrays. Error given on arrays is following:




    InvalidCastException: Cannot cast from source type to destination
    type.
    Firebase.Database.Internal.Utilities.Validation.ValidateWritableObject
    (System.Object object)




    Any help will be much appreciated...










    share|improve this question

























      0












      0








      0








      I am trying to store an Int32 Array and bool Array to Firebase but it isn't working for me. I have searched on various places, but couldn't find a solution. Can anyone tell me, how one can store an array to Firebase Real-time DB from Unity.



      I am using System.Reflection to get all the public static fields of the class UserPrefs.



      Here is the code, I am trying to do this job...



              System.Type type = typeof(UserPrefs);
      FieldInfo fields = type.GetFields();
      foreach (var field in fields) {
      if (user != null)
      dbReference.Child("users").Child(user.UserId).Child(field.Name).SetValueAsync(field.GetValue(null));
      else
      Debug.LogError("There is no user LoggedIn to write...");
      }


      Above code saves all values other than arrays. Error given on arrays is following:




      InvalidCastException: Cannot cast from source type to destination
      type.
      Firebase.Database.Internal.Utilities.Validation.ValidateWritableObject
      (System.Object object)




      Any help will be much appreciated...










      share|improve this question














      I am trying to store an Int32 Array and bool Array to Firebase but it isn't working for me. I have searched on various places, but couldn't find a solution. Can anyone tell me, how one can store an array to Firebase Real-time DB from Unity.



      I am using System.Reflection to get all the public static fields of the class UserPrefs.



      Here is the code, I am trying to do this job...



              System.Type type = typeof(UserPrefs);
      FieldInfo fields = type.GetFields();
      foreach (var field in fields) {
      if (user != null)
      dbReference.Child("users").Child(user.UserId).Child(field.Name).SetValueAsync(field.GetValue(null));
      else
      Debug.LogError("There is no user LoggedIn to write...");
      }


      Above code saves all values other than arrays. Error given on arrays is following:




      InvalidCastException: Cannot cast from source type to destination
      type.
      Firebase.Database.Internal.Utilities.Validation.ValidateWritableObject
      (System.Object object)




      Any help will be much appreciated...







      arrays firebase unity3d firebase-realtime-database






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 6 '18 at 6:58









      Farhan ShehzadFarhan Shehzad

      34




      34
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need a class like this.



          public class MyClass
          {
          public int intArray = new int[10];
          }


          Then you can write that object to the Firebase like this.



          public void WriteArrays()
          {
          MyClass temp = new MyClass();

          for (int i = 0; i < temp.intArray.Length; i++)
          {
          temp.intArray[i] = i;
          }

          databaseReference.Child("MyArrayNode").SetRawJsonValueAsync(JsonUtility.ToJson(temp));
          }


          databaseReference is a reference to your root.



          Same way you can do this for your bool also.






          share|improve this answer


























          • There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

            – Farhan Shehzad
            Nov 6 '18 at 10:34











          • Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

            – Bhavin Panara
            Nov 6 '18 at 10:49











          • So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

            – Farhan Shehzad
            Nov 6 '18 at 12:52













          • I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

            – Bhavin Panara
            Nov 14 '18 at 4:26











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53167083%2fhow-to-store-an-array-to-firebase-via-unity-sdk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You need a class like this.



          public class MyClass
          {
          public int intArray = new int[10];
          }


          Then you can write that object to the Firebase like this.



          public void WriteArrays()
          {
          MyClass temp = new MyClass();

          for (int i = 0; i < temp.intArray.Length; i++)
          {
          temp.intArray[i] = i;
          }

          databaseReference.Child("MyArrayNode").SetRawJsonValueAsync(JsonUtility.ToJson(temp));
          }


          databaseReference is a reference to your root.



          Same way you can do this for your bool also.






          share|improve this answer


























          • There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

            – Farhan Shehzad
            Nov 6 '18 at 10:34











          • Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

            – Bhavin Panara
            Nov 6 '18 at 10:49











          • So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

            – Farhan Shehzad
            Nov 6 '18 at 12:52













          • I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

            – Bhavin Panara
            Nov 14 '18 at 4:26
















          0














          You need a class like this.



          public class MyClass
          {
          public int intArray = new int[10];
          }


          Then you can write that object to the Firebase like this.



          public void WriteArrays()
          {
          MyClass temp = new MyClass();

          for (int i = 0; i < temp.intArray.Length; i++)
          {
          temp.intArray[i] = i;
          }

          databaseReference.Child("MyArrayNode").SetRawJsonValueAsync(JsonUtility.ToJson(temp));
          }


          databaseReference is a reference to your root.



          Same way you can do this for your bool also.






          share|improve this answer


























          • There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

            – Farhan Shehzad
            Nov 6 '18 at 10:34











          • Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

            – Bhavin Panara
            Nov 6 '18 at 10:49











          • So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

            – Farhan Shehzad
            Nov 6 '18 at 12:52













          • I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

            – Bhavin Panara
            Nov 14 '18 at 4:26














          0












          0








          0







          You need a class like this.



          public class MyClass
          {
          public int intArray = new int[10];
          }


          Then you can write that object to the Firebase like this.



          public void WriteArrays()
          {
          MyClass temp = new MyClass();

          for (int i = 0; i < temp.intArray.Length; i++)
          {
          temp.intArray[i] = i;
          }

          databaseReference.Child("MyArrayNode").SetRawJsonValueAsync(JsonUtility.ToJson(temp));
          }


          databaseReference is a reference to your root.



          Same way you can do this for your bool also.






          share|improve this answer















          You need a class like this.



          public class MyClass
          {
          public int intArray = new int[10];
          }


          Then you can write that object to the Firebase like this.



          public void WriteArrays()
          {
          MyClass temp = new MyClass();

          for (int i = 0; i < temp.intArray.Length; i++)
          {
          temp.intArray[i] = i;
          }

          databaseReference.Child("MyArrayNode").SetRawJsonValueAsync(JsonUtility.ToJson(temp));
          }


          databaseReference is a reference to your root.



          Same way you can do this for your bool also.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 6:42









          marc_s

          582k13011231269




          582k13011231269










          answered Nov 6 '18 at 9:18









          Bhavin PanaraBhavin Panara

          17418




          17418













          • There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

            – Farhan Shehzad
            Nov 6 '18 at 10:34











          • Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

            – Bhavin Panara
            Nov 6 '18 at 10:49











          • So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

            – Farhan Shehzad
            Nov 6 '18 at 12:52













          • I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

            – Bhavin Panara
            Nov 14 '18 at 4:26



















          • There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

            – Farhan Shehzad
            Nov 6 '18 at 10:34











          • Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

            – Bhavin Panara
            Nov 6 '18 at 10:49











          • So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

            – Farhan Shehzad
            Nov 6 '18 at 12:52













          • I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

            – Bhavin Panara
            Nov 14 '18 at 4:26

















          There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

          – Farhan Shehzad
          Nov 6 '18 at 10:34





          There can be many other arrays of other types. I need to make the code generic. It's not a good solution to make class for all possible arrays, iterate in a loop and then save to Firebase. All this will be done again while loading the data.

          – Farhan Shehzad
          Nov 6 '18 at 10:34













          Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

          – Bhavin Panara
          Nov 6 '18 at 10:49





          Passing json to firebase is the preferred way to communicate with firebase. Design according to it.

          – Bhavin Panara
          Nov 6 '18 at 10:49













          So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

          – Farhan Shehzad
          Nov 6 '18 at 12:52







          So, you are saying I need to design my own json object, in case I want to save arrays to Firebase. If so, can you please give me any example code to make a json for arrays?

          – Farhan Shehzad
          Nov 6 '18 at 12:52















          I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

          – Bhavin Panara
          Nov 14 '18 at 4:26





          I am saying that whenever you want to pass more than one thing to Firebase, use json. You can use in-built json utility of Unity.

          – Bhavin Panara
          Nov 14 '18 at 4:26




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53167083%2fhow-to-store-an-array-to-firebase-via-unity-sdk%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin