UICollectionView 的 99% CPU 使用率 - 重要位置更改未调用 "didUpdateLocations"

99% CPU usage of UICollectionView - Significant Location Change not calling "didUpdateLocations"

有效的是应用程序在位置发生重大变化时启动到后台。在 AppDelegate 中,我检查 UIApplicationLaunchOptionsLocationKey 并启动位置管理器:

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 50
locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoringSignificantLocationChanges()

初始视图控制器是 UITabBarController,显示 UICollectionViewController

当我设置 UICollectionView 中没有单元格时,一切正常,locationManagerdidUpdateLocations 被调用。

然而,当 UICollectionViewnumberOfItemsInSection returns 1 时,didUpdateLocations 不会被调用。相反,UICollectionView 有 99% CPU 使用率几分钟,直到应用程序崩溃。

我删除了单元格中的所有控件和所有代码,所以它现在是一个空单元格,但仍然出现这种情况。

在 Time Profiler 中,我看到这与 UICollectionViewData setLayoutAttributes.

有关

这是怎么回事?

更新一:setLayoutAttributes.

下有堆栈

更新 2: 该应用程序运行良好,在用户正常启动时从未崩溃。

您比本网站上的任何人都更了解代码,但是这看起来很可疑:

你是 运行 对观察者的某种循环,不管它们是什么。 看起来每个它都在做一个回调,那是在做某种需要提交的事务,作为提交的一部分,它正在显示一些东西,作为其中的一部分,它正在做一堆布局,子视图等。 这基本上耗尽了 所有 的时间。

您可能希望在完成之前关闭显示更新。

有趣的是,解决方案是禁用一次重大位置更改,运行 应用程序:

locationManager.allowsBackgroundLocationUpdates = false
locationManager.stopMonitoringSignificantLocationChanges()

重新启用它和 运行 应用程序后,它运行良好:

locationManager.allowsBackgroundLocationUpdates = true
locationManager.startMonitoringSignificantLocationChanges()

我尝试了所有方法,从删除第 3 方框架到将应用程序剥离到最少的代码,是的,我还清理了构建文件夹 100 次。

可能 iOS 应用程序注册有问题以通知重大位置更改。希望这可以节省我花在弄清楚这个问题上的时间。