【问题标题】:Linq in monotouch (debugging on device)Monotouch中的Linq(在设备上调试)
【发布时间】:2012-12-28 19:15:53
【问题描述】:

我正在尝试运行 Windows Azure 移动服务查询(使用 SDK 的 xamarins monotouch 分支)。

这段代码在模拟器上运行良好,但在设备上崩溃了:

this.table.Where (a => a.Sequence == sequence).Where (a => a.Week == week).ToListAsync()
                .ContinueWith (t =>
            {
                this.items = t.Result;
                this.tableView.ReloadData ();
                IsUpdating = false;
            }, scheduler);

我得到的错误是:

调用的目标已抛出异常。 ---> System.Exception:尝试 JIT 编译方法 'System.Linq.jvm.Runner:GetDelegate ()' 同时运行 --aot-only。

我设法使它工作的唯一一件事就是删除 where 条件。 这工作得很好,除了我(显然)没有根据需要过滤结果。

我应该如何重写我的代码以使其在实际的 iOS 设备上运行?

更新: table 是 *IMobileServiceTable *

类型的类变量

week 和 sequence 都是 int 类型。

Activity 是一个 POCO 类。

    public class Activity
{
        public int ID {
            get;
            set;
        }
        public string Name {
            get;
            set;
        }

        public int CaloricRequirementMin {
            get;
            set;
        }

        public int CaloricRequirementMax {
            get;
            set;
        }

        public string Difficulty {
            get;
            set;
        }

        public int PlanId {get;set;}

        public string Type {
            get;
            set;
        }

        public int Sequence {
            get;
            set;
        }
        public int Week {
            get;
            set;
        }

        public int SubscriptionActivityId {
            get;
            set;
        }
}

我已仔细检查以确保这些都已填充。

它在模拟器上完美无瑕。

【问题讨论】:

  • 您是否必须删除两个 where 条件?它是否以单个 where 条件运行? Monotouch(在设备上)在 JIT 编译时存在局限性。
  • 是的。对于单一条件,它也会因相同的错误而失败。删除这两个条件是我能够成功运行它的唯一方法。
  • 只是问问题,因为我不能简单地测试代码。同步 .ToList() 是否有效?可以是您的表/对象定义吗?
  • SDK没有提供同步ToList方法:s
  • 抱歉问了这个愚蠢的问题。我确实认为问题不在这部分代码中,而更有可能在表/对象定义中。您的对象是否有子关系或其他依赖关系?这篇文章似乎相关:siaqodb.com/forum/index.php?topic=365.0

标签: linq xamarin.ios jit aot azure-mobile-services


【解决方案1】:

MonoTouch Ahead Of Time (AOT) 编译器的全部意义在于避免 Apple 不允许在 iOS 中编译的问题。它是多个安全策略之一,还有签名的可执行文件、应用程序审查、沙盒等。不幸的是,某些 LINQ 表达式需要 JIT 编译,因此无法在设备上运行。

所有 LINQ 表达式都可以转换为非 LINQ,通常是迭代代码。在转换为迭代之前,您可以考虑一些可能有效的 LINQ 替代方法,例如 Any() 表达式。

【讨论】:

    【解决方案2】:

    最后我不得不修改我的代码以使用带有字符串查询而不是 linq 表达式的 ReadAsync。

    this.table.ReadAsync(query)
                    .ContinueWith (t =>
                {
                        items = (from item in t.Result.GetArray()
                                 let act = item.GetObject()
                        select new Activity{
                            ID= Convert.ToInt32(act.GetNamedNumber("id")),
                            Name= act.GetNamedString("Name"),
                            SubscriptionActivityId = act.ContainsKey("SubscriptionActivityId") ? Convert.ToInt32(act.GetNamedNumber("SubscriptionActivityId")) : 0
                        }).ToList();
    
    
                        this.tableView.ReloadData ();
                    IsUpdating = false;
                }, scheduler);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多