- Home /
Update Parse.com Database OnApplicationQuit
I need to update the Database on Parse.com when a user quits the game. I have the inventory class check OnApplicationQuit() and when it does, it calls a SaveInventory function which saves to the Parse.com Database.
I know that the SaveInventory function works, if I call that at any point during the normal game, it saves as it should. When OnApplication is Called, Every Debug.Log in SaveInventory is called as it should be. Does anyone know how I can update the database when a user quits.
Here is my SaveInventory function:
public void SaveInventory(DictionaryOfStringAndInt inventory) {
Debug.Log("Save Starting");
myInventory = inventory;
inventoryData.SaveAsync().ContinueWith(t => {
List<string> slots = new List<string>() {
"slot01", "slot02", "slot03", "slot04", "slot05", "slot06",
"slot07", "slot08", "slot09", "slot10", "slot11", "slot12",
"slot13", "slot14", "slot15", "slot16", "slot17", "slot18",
"slot19", "slot20"
};
int slotsIndex = 0;
inventoryData["coins"] = myInventory["coins"];
foreach (KeyValuePair<string, int> item in myInventory) {
if (item.Key == "coins") continue;
inventoryData[slots[slotsIndex] + "Name"] = item.Key;
inventoryData[slots[slotsIndex] + "Amount"] = item.Value;
Debug.Log(slots[slotsIndex] + "Name" +": " + item.Key + " - " + slots[slotsIndex] + "Amount" + ": " + item.Value);
slotsIndex++;
}
inventoryData.SaveAsync().ContinueWith(tt => {
Debug.Log("WHAT");
});
Debug.Log("Save Completed");
});
}
Have you tried to use OnDisable() ins$$anonymous$$d of OnApplicationQuit()?
Answer by urban2000 · Mar 08, 2015 at 05:29 PM
Hi,
I had the same issue with my Application (on Android). To get it to work I used OnApplicationPause instead.
public void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus == true)
{
SaveToParse();
}
}
However, I could not use the normal SaveAsync() API for saving. Instead I did a synchronous save through the REST API (https://parse.com/docs/rest).
In the synchronous save you have to upload your data as JSON.
WebClient client = new WebClient();
client.Headers.Add("X-Parse-Application-Id", ApplicationId);
client.Headers.Add("X-Parse-REST-API-Key", RestApiKey);
client.Headers.Add("Content-Type", "application/json");
string json = yourParseObjAsJson;
byte[] data = Encoding.UTF8.GetBytes(json);
var uri = string.Format("https://api.parse.com/1/classes/{0}/{1}", obj.ClassName, obj.ObjectId);
client.UploadData(uri, "PUT", data);
I've tried this too, but I alwas get this error:
System.Net.WebException: Error getting response stream (Write: The authentication or decryption has failed.): SendFailure ---> System.IO.IOException: The authentication or decryption has failed. ---> $$anonymous$$ono.Security.Protocol.Tls.TlsException: Invalid certificate received from server. Error code: 0xffffffff800b010a
You could ignore the SSL certificate validation by doing this.
ServicePoint$$anonymous$$anager.ServerCertificateValidationCallback = (p1, p2, p3, p4) => true;
Your answer
Follow this Question
Related Questions
Unity Vs ParseCloud Vs Server 0 Answers
Cloud recognition in Vuforia 0 Answers
Parse and Unity on Windows Store App 1 Answer
Google Cloud Messaging for Android 1 Answer