ios调用高德地图定位报错

发布于:2024-07-06 ⋅ 阅读:(16) ⋅ 点赞:(0)

错误信息如下:

Thread Performance Checker: Thread running at User-interactive quality-of-service class waiting on a lower QoS thread running at Default quality-of-service class. Investigate ways to avoid priority inversions

PID: 1668, TID: 15380

Backtrace

=================================================================

3   Foundation                          0x0000000113be3985 -[_NSThreadPerformInfo wait] + 64

4   Foundation                          0x0000000113be5da5 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 907

5   yydj                                0x0000000100fa1e89 -[AMapLocationManager startUpdatingLocation] + 98

6   yydj                                0x0000000100f15a68 $s4yydj14ViewControllerC8locationyyF + 408

7   yydj                                0x0000000100f1557b $s4yydj14ViewControllerC11viewDidLoadyyF + 1675

8   yydj                                0x0000000100f158bc $s4yydj14ViewControllerC11viewDidLoadyyFTo + 28

9   UIKitCore                           0x000000012a7bafee -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 80

10  UIKitCore                           0x000000012a7c10e6 -[UIViewController loadViewIfRequired] + 1342

11  UIKitCore                           0x000000012a6dcb2b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 159

12  UIKitCore                           0x000000012a6dce5f -[UINavigationController _startTransition:fromViewController:toViewController:] + 210

13  UIKitCore                           0x000000012a6de035 -[UINavigationController _startDeferredTransitionIfNeeded:] + 856

14  UIKitCore                           0x000000012a6df495 -[UINavigationController __viewWillLayoutSubviews] + 136

15  UIKitCore                           0x000000012a6bbde2 -[UILayoutContainerView layoutSubviews] + 207

16  UIKit                               0x0000000150bf9c73 -[UILayoutContainerViewAccessibility layoutSubviews] + 51

17  UIKitCore                           0x000000012b88af3c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2138

18  QuartzCore                          0x000000010e313775 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 527

19  QuartzCore                          0x000000010e31f5a1 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 67

20  QuartzCore                          0x000000010e23ecf4 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 706

21  QuartzCore                          0x000000010e27982a _ZN2CA11Transaction6commitEv + 728

22  UIKitCore                           0x000000012b1c9d8f __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 34

23  CoreFoundation                      0x000000010f4f5dea __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12

24  CoreFoundation                      0x000000010f4f559d __CFRunLoopDoBlocks + 391

25  CoreFoundation                      0x000000010f4efe7c __CFRunLoopRun + 940

26  CoreFoundation                      0x000000010f4ef6ed CFRunLoopRunSpecific + 557

27  GraphicsServices                    0x000000011c76f08f GSEventRunModal + 137

28  UIKitCore                           0x000000012b1aa6ee -[UIApplication _run] + 972

29  UIKitCore                           0x000000012b1af16e UIApplicationMain + 123

30  UIKitCore                           0x000000012a22ab73 __swift_destroy_boxed_opaque_existential_1Tm + 10307

31  yydj                                0x0000000100f20f5b $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 123

32  yydj                                0x0000000100f20ed7 $s4yydj11AppDelegateC5$mainyyFZ + 39

33  yydj                                0x0000000100f21008 main + 24

Message from debugger: killed

回答:

这个错误信息表明你的应用在执行用户交互质量的服务时,阻塞在了一个较低优先级的线程上,这导致了优先级倒置。特别是,你的应用在调用`AMapLocationManager`的`startUpdatingLocation`时发生了这个问题。这可能会导致应用性能问题。以下是一些可能的解决方法:

### 1. 在后台线程中启动位置更新

`AMapLocationManager`的`startUpdatingLocation`可能是一个耗时操作,应该在后台线程中运行,而不是在主线程中。你可以使用`DispatchQueue`来处理:

```swift
DispatchQueue.global(qos: .background).async {
    self.locationManager.startUpdatingLocation()
}
```

### 2. 确保UI更新在主线程中进行

所有的UI更新都应该在主线程中进行。确保你所有涉及UI更新的代码都在主线程中运行:

```swift
DispatchQueue.main.async {
    // 更新UI
}
```

### 3. 优化`viewDidLoad`方法

确保`viewDidLoad`方法中没有长时间运行的任务。如果有,将这些任务移到后台线程中执行:

```swift
override func viewDidLoad() {
    super.viewDidLoad()
    
    DispatchQueue.global(qos: .background).async {
        self.locationManager.startUpdatingLocation()
    }
    
    // 其他初始化代码
}
```

### 4. 检查线程优先级

确保线程优先级设置正确。可以使用`qos`(质量服务类)来设置线程优先级:

```swift
DispatchQueue.global(qos: .userInitiated).async {
    // 用户交互相关的任务
}

DispatchQueue.global(qos: .background).async {
    // 后台任务
}
```

### 5. 避免死锁

确保你没有在主线程中执行可能会导致死锁的操作,例如同步调用`DispatchQueue`,特别是在涉及到UI更新和网络请求时。

### 6. 其他性能优化

- 优化`viewDidLoad`方法,确保只做必要的初始化工作。
- 使用`Instruments`工具分析性能瓶颈,特别是线程的优先级和阻塞情况。
- 评估是否有不必要的阻塞调用,重构代码以避免长时间占用主线程。

### 代码示例

```swift
import UIKit
import AMapLocationKit

class ViewController: UIViewController {
    var locationManager: AMapLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 初始化位置管理器
        locationManager = AMapLocationManager()
        
        // 在后台线程中启动位置更新
        DispatchQueue.global(qos: .background).async {
            self.locationManager.startUpdatingLocation()
        }
        
        // 其他初始化代码
    }
}
```


网站公告

今日签到

点亮在社区的每一天
去签到