最新消息:20210917 已从crifan.com换到crifan.org

给Your Second iOS App: BirdWatching进行代码和性能的优化

iOS crifan 2043浏览 0评论

之前已经实现了Your Second iOS App的基本的扩展功能了,并且也放出了代码了:

【源码下载】在实现了一系列的扩展功能后的Your Second iOS App的源码下载

接下来,继续根据:

Your Second iOS App – Next Steps – Add More Functionality

的建议:

Optimize the code. High performance is critical to a good user experience on iOS. You should learn to use the various performance tools provided with Xcode, such as Instruments, to tune your app so that it minimizes its resource requirements.

去优化代码和性能。


1.之前就简单看过:

Performance Starting Point

的介绍,只是没去细看。

2.现在找到了,其他一些和性能优化相关的官网资料:

Performance Overview

其实这些资料,都可以在:

Performance

中找到的。

3.知道了,想要优化的话,主要还是通过Xcode中的Instruments去做的,所以先去学习一下:

Instruments User Guide

4.了解了基本的知识点后:

Xcode中的Instruments的学习和总结

剩下的就是去折腾了:


1. 打开Instruments:

xcode open developer tool instruments

 

2.想要参考“To run Instruments while building your code"去找那个”scheme editor“,结果找了半天没找到。

后来还是在:

Xcode->Product->Edit Scheme:

product edit scheme

中找到的“Edit Scheme”,打开后是:

edit scheme

然后选中Profile xxx,对于其中的Instruments选中为”ask on launch“:

profile app instrument ask on launch

这样会使每次启动Instruments时,都会跳出对话框让你选择要打开的类型。

3.可以设置Instruments在Dock中保留:

keep in dock

这样以后就可以不启动Xcode的情况下,也可以直接点击而启动Instruments了。

4.通过Instruments收集数据的流程是:

  • 选择追踪(trace)模板
  • 选择要调试的目标app
  • 收集你要的数据
  • 检查你所收集的数据

5.接着就是实践了:

先选中一种类型,此处选了个内存泄露:

choose leaks

然后去选择目标Choose Target,但是竟然没有找到我所需要的当前项目BirdWatching的app或进程,然后就是一堆折腾,具体折腾过程参考:

【已解决】Xcode的Instruments中,选择Choose Target后,找不到当前项目对应的app程序(BirdWatching),看不到收集的app的数据

6.可以搜集数据,显示结果之后,再去试了试,对于程序,执行了很多操作,最后终于找到了一处的内存泄露:

found one leak

剩下的,就是如何看懂这些数据,并找到程序中出现内存泄露的相关代码并解决掉此内存泄露的问题了。

7.但是,随着程序执行,上述搜集的数据是不停变化的,所以,为了找到刚才内存泄露的位置,先去暂停当前的record。

然后折腾了下,发现点击某项,鼠标移动经过该项,会显示出一个小箭头:

found one arrow

然后点击了小箭头后,跳转到详细信息:

jump to leak backtrace

 

8.

转载请注明:在路上 » 给Your Second iOS App: BirdWatching进行代码和性能的优化

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.183 seconds, using 23.45MB memory