I use a Result object to be to handle both positive and negative results when querying my server side code in my:
namespace GenerationLondon
{
public abstract class Result<E, S>
{
private Result() {}
public abstract R fold<R>(Func<E, R> left, Func<S, R> right);
public abstract void consume (Action<E> left, Action<S> right);
public static Result<E, S> success(S value)
{
return new Success (value);
}
public static Result<E, S> error(E value)
{
return new Error (value);
}
public class Success : Result<E, S>
{
private S success;
public Success(S success)
{
this.success = success;
}
public override R fold<R> (Func<E, R> left, Func<S, R> right)
{
return right.Invoke(success);
}
public override void consume (Action<E> left, Action<S> right)
{
right.Invoke (success);
}
}
public class Error : Result<E, S>
{
private E error;
public Error(E error)
{
this.error = error;
}
public override R fold<R> (Func<E, R> left, Func<S, R> right)
{
return left.Invoke (error);
}
public override void consume (Action<E> left, Action<S> right)
{
left.Invoke (error);
}
}
}
}
After a user successfully logs in, I fire off 2 requests to the server to gather some information but I keep receiving the following exception:
System.InvalidProgramException: Invalid IL code in GenerationLondon.LoginPage/<OnLogin>c__asyncD/<OnLogin>c__AnonStorey14:<>m__1 (System.Collections.Generic.List`1<GenerationLondon.UserEvent>): IL_0008: stfld 0x040000ef
at GenerationLondon.Result`2+Success[System.String,System.Collections.Generic.List`1[GenerationLondon.UserEvent]].consume (System.Action`1 left, System.Action`1 right) [0x00008] in /Users/tom/Documents/workspace/GenerationLondon/GenerationLondon/Result.cs:39
at GenerationLondon.LoginPage+<OnLogin>c__asyncD.MoveNext () [0x001df] in /Users/tom/Documents/workspace/GenerationLondon/GenerationLondon/Views/LoginPage.xaml.cs:30
at --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIKitSynchronizationContext.cs:24
at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/Foundation/NSAction.cs:163
at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:77
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2320/1f068b49/source/maccore/src/UIKit/UIApplication.cs:61
at GenerationLondon.iOS.Application.Main (System.String[] args) [0x00008] in /Users/tom/Documents/workspace/GenerationLondon/iOS/Main.cs:17
The code in question is as follows:
result.consume(async () => {
Result<string, Details> detailsResult = await restService.getDetails ();
Result<string, List<Event>> eventsResult = await restService.getEvents();
eventsResult.consume(async error => await DisplayAlert("Problem communicating with the server", error, "Ok"), events => {
detailsResult.consume(async error => await DisplayAlert("Problem communicating with the server", error, "Ok"), details => {
loginManager.ShowMainPage (details, events);
});
});
}, async error => await DisplayAlert("Login Issue", error, "Ok"));
The code works fine when run individually, but this exception consistently occurs when trying to process eventsResult after introducing the detailsResult. Both calls are returning successfully, and the exception itself is coming from return right.Invoke(success);
The only thing I can think of is Mono is having issues deriving problems with the types