I am calling a Parse cloud function which always works the first time, but subsequent calls only occasionally complete (meaning neither 'Error', 'Success', or 'Refresh Complete' is printed):
IEnumerator RefreshScores()
{
Debug.Log("Asking Parse for scores...");
var parameters = new Dictionary<string, object>();
Task task = ParseCloud.CallFunctionAsync<IList<IDictionary<string, object>>>("getFriendsScores", parameters).ContinueWith(t =>
{
if (t.IsFaulted)
{
Debug.Log("Error!");
}
else
{
Debug.Log("Success!");
}
});
while (!task.IsCompleted) yield return null;
Debug.Log("Refresh Complete!");
}
I don't know why this is happening, but as an attempt to get around the inconsistency I tried passing a cancellation token to give up manually after x seconds, but that did not seem to have an effect either :(
Any help on the issue would be greatly appreciated.
As it turns out, the problem is unrelated to Parse, and rather caused by my misunderstanding of how Couroutines lifetimes are managed in Unity.
I was calling StartCouroutine(MySingleton.Instance.RefreshScores());
from an object that was disabled shortly afterwards, which in turn was ruining the co-routine.
The solution in my case was to create a public wrapper function on my singleton which then calls the co-routine from the correct object.