Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JIT error with LINQ OrderBy using C# on iOS

I'm receiving the following error on my iOS device:

ExecutionEngineException: Attempting to JIT compile method 'System.Linq.OrderedEnumerable1<System.Collections.Generic.KeyValuePair2>:GetEnumerator ()' while running with --aot-only.

I'm using Unity3D, and I know the error is caused because LINQ expressions have issues with ordering value types when compiling Unity to iOS. Because (I think) that the expression attempts to use reflection to instantiate a new type which implements the IComparer<TKey> interface. This will work for reference types, but not value types on a Unity iOS build.

So my thought was that since I know in this situation I'm always trying to order a collection of ints. That I could bypass the generic ICompare<TKey> and just create my own custom comparer.

public class IntCompare : Comparer<int> {

    public override int Compare (int x, int y)
    {
       return x - y;
    }
}

However, using OrderBy still gives me the error. Is there something I'm not understanding on why my method doesn't work?

My expression:

OptimizeMaxCommitList(members
                            .OrderBy((memberid) => memberid.Value, new IntCompare())
                            .Skip(1)
                            .ToDictionary(pair => pair.Key, pair => pair.Value)
                    ,maxCommit);
like image 786
MichaelTaylor3D Avatar asked May 10 '13 15:05

MichaelTaylor3D


1 Answers

Most of LINQ extension methods from LINQ for Collections are not working with IEnumerables on iOS since they require the AOT runtime compiler which is not supported.

However there is a LINQ for iOS library on Asset Store that is similar to LINQ, but doesn't require a runtime compiler. So you can use it on iOS.

like image 66
Anatoliy Landyshev Avatar answered Sep 27 '22 16:09

Anatoliy Landyshev