As mentioned in my last post, I am working on a Unity game that takes advantage of Parse for Asyncronous multiplayer. Well one nice feature of parse is that it uses Tasks to handle its asynchronicity.
Tasks are very much like JS promises (except they are type-safe) and return when the operation has completed. For example:
[code lang="csharp"] obj.SaveAsync().ContinueWith(task => { if (task.IsCanceled) { // the save was cancelled. } else if (task.IsFaulted) { AggregateException exception = task.Exception; } else { // the object was saved successfully. } }); [/code]
You can chain tasks together like so:
[code lang="csharp"]
var query = new ParseQuery
query.FindAsync().ContinueWith(t =>
{
var students = t.Result;
IEnumerator
The problem is that if you want to catch errors you must manually check the task return for errors inside each handler. This seemed wasteful to me as all I wanted was a global error handler for that particular task chain, such as can be achieved with Javascript Promises.
Fortunately someone else also had noticed this problem and solved it:
[code lang="csharp"] Task.Factory.StartNew(StartBusyIndicator) .Then(task => GetWebResponseAsync(url)) .Then(task => Console.WriteLine(task.Result.Headers)) .Finally(ExceptionHandler, StopBusyIndicator); [/code]
The only problem is that when I tried to implement his C# library Unity started throwing the dreaded Internal Compiler Error:
Internal compiler error. See the console log for more information. output was:
** Unhandled Exception: System.ArgumentNullException: Argument cannot be null.**
It took me a while to work out what was going on. I managed to simplify the entire problem down to this simple example:
[code lang="csharp"]
public static class TaskHelpers
{
public static void Then
}
}
[/code]
I posted about this on the Unity forum on Parse's Forum and even on Parse's bug tracking system but no one was interested, Parse even told me its not a Parse issue.
It took me quite a while to work out what was going on, but I eventually worked out that if I separated the handler from the Continue With call it would work:
[code lang="csharp"]
public static class TaskHelpers
{
public static void Continue
}
}
[/code]
Huzzah! It compiles. So I guess this is a lesson learnt. With the Unity Mono compiler, if you are getting Internal Compiler errors then perhaps try separating out the lambdas into variables.