Search Unity

Need help with Firebase

Discussion in 'Scripting' started by squigglyo, Nov 24, 2016.

  1. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    Hey there folks

    Im implementing Firebase in to my app. Ive managed to figure out the GET and the SET, but the PATCH is proving difficult.

    Here is how I use the GET.
    Code (CSharp):
    1. string str = "https://" + appID + ".firebaseio.com/teams.json?orderBy=\"code\"&equalTo=\"" + codeText.text + "\"";
    2.  
    3. WWW url = new WWW (str);
    4.  
    5. StartCoroutine (WaitForRequest (url));
    This returns a string, which I can easily parse and read.
    This one in particular returns the result that matches the 'code' found in the codeText.text. Allowing me to check if that value exists in the database.

    Here is my SET
    Code (CSharp):
    1. string url = "https://" + appID + ".firebaseio.com/players.json";
    2.  
    3.         string jsonConvertedForm = "{\"firstName\":\"Scott\",\"secondName\":\"Jones\"}";
    4.  
    5.         Dictionary <string, string> headers = new Dictionary<string, string>();
    6.         headers.Add ("Content-Type", "application/json");
    7.  
    8.         byte[] bytePostData = System.Text.Encoding.UTF8.GetBytes (jsonConvertedForm);
    9.         WWW www = new WWW (url, bytePostData, headers);
    10.  
    11.         StartCoroutine (WaitForRequest (www));
    This one creates a new player, setting the first and last names.

    So, I can retrieve data, and also add a new entry in to the database.

    However, I cant seem to figure out the JSON to PATCH.

    My best attempt so far
    Code (CSharp):
    1. string url = "https://" + appID + ".firebaseio.com/players/-KX8i-OhbOmpbdyTPCg0.json";
    2.  
    3.         string jsonConvertedForm = "{\"firstName\":\"NotScott\",\"secondName\":\"NotJones\"}";
    4.  
    5.         Dictionary <string, string> headers = new Dictionary<string, string>();
    6.         headers.Add ("Content-Type", "application/json");
    7.  
    8.         byte[] bytePostData = System.Text.Encoding.UTF8.GetBytes (jsonConvertedForm);
    9.         WWW www = new WWW (url, bytePostData, headers);
    10.  
    11.         StartCoroutine (WaitForRequest (www));
    However, this creates a new entry inside that player which, inside that has the updated values.

    So instead of looking like this
    • -KX8i-OhbOmpbdyTPCg0
      • firstName : NotScott
      • lastName : NotJones
    Instead it looks like this
    • -KX8i-OhbOmpbdyTPCg0
    • -KXKrUGT4_AGHhVXSGXa
    • firstName : NotScott
    • lastName : NotJones
    • firstName : Scott
    • lastName : Jones
    My client is very particular and wants this done in JSON. I assume its possible, since i got the PUT to work. Im assuming theres some special way i can do the JSON that im not quite seeing.​