ios调用高德地图定位报错

news/2024/7/8 1:43:27 标签: ios, swift

错误信息如下:

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()
        }
        
        // 其他初始化代码
    }
}
```


http://www.niftyadmin.cn/n/5535881.html

相关文章

linux指令tcpdump

目录 1. 安装 tcpdump 2. 基本用法 捕获所有网络接口上的数据包 捕获特定网络接口上的数据包 捕获特定协议的数据包 3. 示例 捕获特定主机的数据包 捕获特定端口的数据包 捕获源或目的 IP 和端口的组合 捕获 ICMP 数据包(如 ping 请求) 捕获并…

35 智能指针

目录 为什么需要智能指针?内存泄露智能指针的使用及原理c11和boost中智能指针的关系RAII扩展学习 1. 为什么需要智能指针? 下面我们先分析一下下面这段程序有没有什么内存方面的问题? int div() {int a, b;cin >> a >> b;if (…

git clone报错RPC failed; curl 92 HTTP/2 stream 7 was not closed cleanly

问题描述 git clone github上的项目报错: RPC failed; curl 92 HTTP/2 stream 7 was not closed cleanly: CANCEL (err 8) 4796 bytes of body are still expected fetch-pack: unexpected disconnect while reading sideband packet early EOF fetch-pack: invalid index-pac…

黑马苍穹外卖技术亮点 详情

1.使用工厂模式和策略模式实现布隆过滤器解决缓存穿透问题 Bitmap Bitmap是一种数据结构,它使用位图来表示数据。在处理大量数据时,Bitmap可以通过将每个数据元素映射到一个位,然后使用位运算来对数据进行操作。 通过使用Bitmap&#xff0c…

FastAPI教程III

本文参考FastAPI教程https://fastapi.tiangolo.com/zh/tutorial 这部分暂无需求的没有记录,仅放置标题。 依赖项 安全性 中间件 你可以向FastAPI应用添加中间件。 ”中间件“是一个函数,它在每个请求被特定的路径操作处理之前,以及在每个…

Python逻辑控制语句 之 判断语句--if语句的基本结构

1.程序执行的三大流程 顺序 分支(判断) 循环 2.if 语句的介绍 单独的 if 语句,就是 “如果 条件成⽴,做什么事” 3.if 语句的语法 if 判断条件: 判断条件成立,执行的代码…

Spring Cloud LoadBalancer基础入门与应用实践

官网地址:https://docs.spring.io/spring-cloud-commons/reference/spring-cloud-commons/loadbalancer.html 【1】概述 Spring Cloud LoadBalancer是由SpringCloud官方提供的一个开源的、简单易用的客户端负载均衡器,它包含在SpringCloud-commons中用…

分子AI预测赛笔记

#AI夏令营 #Datawhale #夏令营 Taks1 跑通baseline 根据task1跑通baseline 注册账号 直接注册或登录百度账号,etc fork 项目 零基础入门 Ai 数据挖掘竞赛-速通 Baseline - 飞桨AI Studio星河社区 启动项目 选择运行环境,并点击确定,没…