本文介绍了Xamarin.iOS在设备上进行调试时,为什么HKAnchoredObjectQuery处于不活动状态然后又被取消激活?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am stumped. I have code that works perfectly on the iOS simulator but when I debug on an actual device the HKAnchoredObjectQuery object is inactive before it runs:<HKAnchoredObjectQuery:0x2816e8780 inactive>

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

And then deactivated after it executes in the results handler:<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上具有了停用"状态.

So, a breakpoint at ExecuteQuery gives me the "inactive" tag on the query object and a breakpoint at if (error != null) gives me "deactivated" on the q.

有人知道为什么会这样吗?就像我说的那样,这一切都可以在iOS Simulator上很好地工作.

Does anyone have any idea why this is happening? Like I said this all works great on the iOS Simulator.

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

Also, I know this is not a read permission problem because I have an HKSampleQuery that returns results of the same type that I am querying for in the included code.

推荐答案

好.因此,我猜它正在按预期方式工作.我正在猜测,因为我的查询是一次运行一次的查询,没有长时间运行的处理程序,因此该查询在处理程序中被执行和停用之前是无效的,因为它不会再次运行.

Ok. So I guess it was working as expected.I'm guessing since my query is a run once query without a long running handler the query is inactive before it is executed and deactivated in the handler because it will not run again.

之所以没有获得任何结果,是因为我有这个谓词只包含Apple设备:HKQuery.GetPredicateForMetadataKey(HKMetadataKey.DeviceManufacturerName, NSPredicateOperatorType.EqualTo, NSObject.FromObject("Apple"));

The reason I was getting no results was because I had this predicate to only include Apple devices:HKQuery.GetPredicateForMetadataKey(HKMetadataKey.DeviceManufacturerName, NSPredicateOperatorType.EqualTo, NSObject.FromObject("Apple"));

不推荐使用DeviceManufacturerName元键.

The use of the DeviceManufacturerName metakey has been deprecated.

执行此操作的正确方法是:HKQuery.GetPredicateForObjectsWithDeviceProperty(HKDevicePropertyKey.Manufacturer, new NSSet<NSString>((NSString) "Apple"));

The proper way to do this is:HKQuery.GetPredicateForObjectsWithDeviceProperty(HKDevicePropertyKey.Manufacturer, new NSSet<NSString>((NSString) "Apple"));

如果Xamarin将该枚举标记为已弃用,那就太好了.和/或在模拟器中使用它时会发出某种警告,因为它在模拟器中可以正常工作.

Would be nice if Xamarin marked that enum as deprecated. And / or there was some sort of warning when using it in the simulator because it worked fine there.

希望这可以在将来为Google节省一些时间.

Hope this saves some googler some time in the future.

这篇关于Xamarin.iOS在设备上进行调试时,为什么HKAnchoredObjectQuery处于不活动状态然后又被取消激活?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:06