Xamarin.iOS 为啥在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用?

Posted

技术标签:

【中文标题】Xamarin.iOS 为啥在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用?【英文标题】:Xamarin.iOS Why is HKAnchoredObjectQuery inactive then deactivated when debugging on device?Xamarin.iOS 为什么在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用? 【发布时间】:2018-12-13 13:29:48 【问题描述】:

我被难住了。 我的代码可以在 ios 模拟器上完美运行,但是当我在实际设备上调试时,HKAnchoredObjectQuery 对象在运行之前处于非活动状态: <HKAnchoredObjectQuery:0x2816e8780 inactive>

然后在结果处理程序中执行后停用: <HKAnchoredObjectQuery:0x2816e8780 deactivated>

我的代码如下所示:

var query = new HKAnchoredObjectQuery(type, compoundPredicate, lastAnchor, nuint.MaxValue, new HKAnchoredObjectResultHandler2(
(q, results, anchor, error) =>

    if (error != null)
    
        _logger.Error("GetCountSinceLastAnchor: description", error.DebugDescription);
    

    var totalCountInThisAnchor = results.Cast<HKQuantitySample>().Sum(sample => sample.Quantity.GetDoubleValue(unit));
    _logger.Information("GetCountSinceLastAnchor: type:type, count: count, lastAnchor:lastAnchor, New Anchor:anchor", type.DebugDescription, totalCountInThisAnchor, lastAnchor, anchor);
    taskCompletionSource.SetResult(Tuple.Create(totalCountInThisAnchor, anchor));
));
_healthKitStore.ExecuteQuery(query);

所以,ExecuteQuery 处的断点给了我 query 对象上的“非活动”标签,if (error != null) 处的断点给了我q 上的“停用”标签。

有人知道为什么会这样吗?就像我说的,这一切都在 iOS 模拟器上运行良好。

另外,我知道这不是读取权限问题,因为我有一个HKSampleQuery,它返回的结果与我在包含的代码中查询的类型相同。

【问题讨论】:

【参考方案1】:

好的。所以我猜它按预期工作。 我猜是因为我的查询是一次运行一次查询,没有长时间运行的处理程序,所以查询在执行之前处于非活动状态,并且在处理程序中停用,因为它不会再次运行。

我没有得到任何结果的原因是因为我有这个谓词只包括 Apple 设备: HKQuery.GetPredicateForMetadataKey(HKMetadataKey.DeviceManufacturerName, NSPredicateOperatorType.EqualTo, NSObject.FromObject("Apple"));

已弃用 DeviceManufacturerName 元键。

正确的做法是: HKQuery.GetPredicateForObjectsWithDeviceProperty(HKDevicePropertyKey.Manufacturer, new NSSet&lt;NSString&gt;((NSString) "Apple"));

如果 Xamarin 将该枚举标记为已弃用,那就太好了。和/或在模拟器中使用它时会出现某种警告,因为它在那里运行良好。

希望这可以在未来节省一些谷歌用户的时间。

【讨论】:

以上是关于Xamarin.iOS 为啥在设备上调试时 HKAnchoredObjectQuery 处于非活动状态然后停用?的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin.iOS:捆绑笔尖没有被复制到模拟器/设备以进行调试/发布

9如何在Xamarin中进行iOS真机调试和发布

在 Xamarin iOS 中调试异步代码

Xamarin.iOS - 设备关闭时推送通知

部署到 iPhone 设备不适用于 Xamarin.iOS,“没有 iOS 签名身份匹配”

Rider IDE 无法在 iPhone 设备上运行项目 Xamarin.iOS