JIT error using LINQ OrderBy using C # on iOS

I get the following error on my iOS device:

ExecutionEngineException: attempted by the JIT compilation method 'System.Linq.OrderedEnumerable 1<System.Collections.Generic.KeyValuePair 2>: GetEnumerator ()' while working with -aot-only.

I am using Unity3D and I know that the error is caused by LINQ expressions having problems with order value types when compiling Unity on iOS. Because (I think) that the expression is trying to use reflection to create a new type that implements the IComparer <TKey> interface. This will work for reference types, but not for value types for iOS Unity builds.

So, I thought that since I know in this situation, I am always trying to order a collection of ints. So that I can get around the generic ICompare <TKey> and just create my own custom mapper.

 public class IntCompare : Comparer<int> { public override int Compare (int x, int y) { return x - y; } } 

However, using OrderBy still gives me an error. I do not understand why my method does not work?

My expression:

 OptimizeMaxCommitList(members .OrderBy((memberid) => memberid.Value, new IntCompare()) .Skip(1) .ToDictionary(pair => pair.Key, pair => pair.Value) ,maxCommit); 
+7
source share
3 answers

Most LINQ extension methods from LINQ for Collections do not work with IEnumerables on iOS, as they require an AOT runtime compiler, which is not supported.

However, there is a LINQ library for iOS in the asset store, which is similar to LINQ but does not require a runtime compiler. This way you can use it on iOS.

+2
source

This does not answer my question directly, but I found a workaround using

 members = sortDictionary(members); OptimizeMaxCommitList(members .Skip(1) .ToDictionary(pair => pair.Key, pair => pair.Value), maxCommit); 

 private Dictionary<string, int> sortDictionary(Dictionary<string, int> members) { List<KeyValuePair<string, int>> list = members.ToList(); list.Sort((firstPair,nextPair) => { return firstPair.Value.CompareTo(nextPair.Value); } ); return list.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); } 
+1
source

By the way, your comparison is broken.

Reading return (x < y) ? x : y; return (x < y) ? x : y; you return the smallest of integers, however, the mapper should provide the result of comparing two objects (read: objects like everything that you process, be it int , bool or string ).

Quoting MSDN , the return value of the comparison is defined as:

  • Less than zero : x less than y.
  • Zero : x is equal to y.
  • Greater than zero : x greater than y.

... were x - the first parameter.

So your comparison should read return x - y; , or the sorting algorithm will never end with the correct results (although it will detect too many iterations and throw an exception, complaining that the comparison is broken).

+1
source

All Articles