The main causes of performance problems are the same and different, but it boils down to memory usage, code efficiency, proper policy logic, code quality, package size, and so on.
But from a user experience perspective, when we are in a place where we have to play an app as a user, what do we care? If be playing a hand swims, do not hope to play above all certainly is playing suddenly flash retreat, do not hope to card next, it is to consume electricity and consume flow to do not hope too serious next, it is to install package to hope to be able a bit smaller finally. The simple classification is as follows:
Fast: avoid lag, fast response speed, reduce user waiting time, meet user expectations. Stability: Do not crash and become unresponsive during user use. Save: save the flow and power consumption, reduce the user cost, avoid the use of hot mobile phone. Small: The installation package is small to reduce the installation cost.
A, fast
Slow application startup and frequent lag in use greatly affect user experience, which should be avoided as far as possible. Caton has many scenarios, which can be divided into four categories: UI drawing, application launch, page jump, and event response. There are many reasons for the lag, but no matter what the reasons and scenarios are, they are ultimately displayed on the device screen to reach the user. In the final analysis, there is a problem with the display.
According to the display principle of iOS system, the basic reasons affecting drawing are as follows:
1. The drawing task is too heavy and it takes too long to draw a frame. 2. The main thread is too busy, and the data is not ready according to the VSYNC signal sent by the system, resulting in frame loss.
Drawing takes too long, and there are tools to help us locate problems. The main thread is responsible for handling user interactions, drawing pixels on the screen, and loading and displaying relevant data, so it’s especially important to avoid any main thread so that your application remains responsive to user actions. To sum up, the main thread does the following:
UI life cycle control 2. System event processing 3. Message processing 4. Interface layout 5. Interface drawing 6. Interface refreshing
In addition, you should try to avoid putting other processing in the main thread, especially complex data calculations and network requests.
Second, the steady
The definition of the stability of an application is very broad. There are many reasons that may affect the stability of an application, such as improper memory usage, poorly considered code exception scenarios, and unreasonable code logic. Two of the most common scenarios are Crash and ANR. These two errors will make the program unusable. Common solutions are as follows:
1. Improve code quality. For example, code review during development, see some code design logic, business rationality, etc. 2. Code static scan tool. Common tools include Clang Static Analyzer, OCLint, Infer, and so on. 3. The Crash monitoring. Record some crash information and abnormal information in time for subsequent analysis and solution. 4.Crash uploading mechanism. After the Crash, try to save the log information to the local computer first and upload the log information when the network is normal next time.
Three,
In mobile devices, batteries are so important that nothing can be done without them. For OS and device developers, power consumption optimization has never stopped in pursuit of longer battery life, while for an app, it is not enough to ignore the power usage problem, especially those classified as “battery killer” apps, which ultimately end up being uninstalled. Therefore, application developers need to minimize battery consumption while fulfilling their requirements.
1.CPU
The CPU is the main hardware used by the application, whether the user is directly using it or not, and the application still consumes CPU resources during background operations and processing of push notifications
The more you compute, the more power you consume. Older devices consume more power to perform the same basic operation, and the amount of computing power consumed depends on different factors
2. The network
Smart network access management makes applications more responsive and helps prolong battery life. When the network is inaccessible, subsequent network requests should be deferred until the network connection is restored. In addition, high broadband consumption operations without WiFi connection should be avoided. Like video streaming, As we all know, cellular wireless systems (LTE,4G,3G, etc.) consume far more power than WiFi signals. The root cause is that LTE devices are based on multi-input, multi-output technology, using multiple concurrent signals to maintain LTE links at both ends. Similarly, all cellular data links are scanned regularly to look for stronger signals. Therefore: we need
1) Check the availability of the appropriate network connection before performing any network operation 2) Continuously monitor the availability of the network and give appropriate feedback when the link status changes 3). Location manager and ** GPS**
We all know that location services are power-hungry, and using GPS to calculate coordinates requires two things:
1) Time lock Each GPS satellite broadcasts a unique 1023 random number every millisecond, Thus, the data transmission rate is 1.024Mbit/s. The GPS receiver chip must be correctly aligned with the satellite’s time lock slot. 2) The frequency lock GPS receiver must calculate the signal error caused by doppler offset caused by the relative motion of the receiver and the satellite
Computing coordinates constantly use CPU and GPS hardware resources, so they quickly drain battery power, so how can they be reduced?
1) Turn off irrelevant features
Determine when you need to track location changes and call the startUpdatingLocation method when tracing is required and the stopUpdatingLocation method when tracing is not required.
Location tracking should also be turned off when the app is running in the background or when the user is not chatting with someone else. In other words, it should be turned off when browsing your media library, checking your friends list, or adjusting your app Settings
2) Use the Internet only when necessary
To maximize battery efficiency, IOS keeps wireless networks off as much as possible. When an app needs to establish a network connection,IOS takes advantage of the opportunity to share a network session with the background app so that low-priority tasks such as push notifications and receiving emails can be handled
The key is that every time a user sets up a network connection, the network hardware stays active for a few more seconds after the connection is completed. Each centralized network communication consumes a lot of power
To mitigate this problem, your software needs to use the network with reservations. Instead of a continuous stream of active data, the network should be used periodically and intensively for short periods. Only then will the network hardware have a chance to shut down
4. The screen
Screens are very power-hungry, and the bigger the screen, the more power-hungry it is. Of course, if your app is running in the foreground and interacting with users, you’re going to use the screen and consume battery
Here are some solutions to optimize screen usage:
1) Animation optimization
Animation is used when the application is in the foreground, and suspended as soon as the application is in the background. Usually, you can do this by listening in UIApplicationWillResignActiveNotification UIApplicationDIdEnterBackgroundNotification notification events to pause or stop the animation, but can be by listening UIApplicationD IdBecomeActiveNotification notification events to restore animation
2) Video optimization
It is best to keep the screen constant during video playback. You can do this using the idleTimerDisabled property of the UIApplication object. Once set to YES, it prevents the screen from sleeping so that it is always on.
Similar to animation, you can release and acquire locks via notifications from the corresponding app
Users always carry mobile phones, so it is particularly important to write codes to save electricity. After all, mobile power of mobile phones is not everywhere. When task complexity cannot be reduced, providing a solution sensitive to battery power and reminding users at appropriate times will make users experience good.
Small four,
The size of an application installation package has no effect on application usage. However, a larger application installation package has a higher threshold for users to download applications, especially on mobile networks, where users have higher requirements on the size of the installation package. Therefore, reducing the size of the installation package enables more users to download and experience products.
Of course, slimming and load reduction are good, but we need to pay attention to the impact of slimming on project maintainability. It is suggested to select skills according to their own projects.
The App installation package consists of resources and executable files. The installation package is optimized from the following three parts.
Resource optimization:
Delete unnecessary resources. Delete duplicate resources. 3. Change uncommon resources to downloads
Compiler optimization:
1. Remove the debug symbol. 2
Executable file optimization:
1. Remove useless code 2. Count library usage and remove useless library 3. 5.ARC->MRC (generally not recommended except in special cases, it will increase maintenance costs)
Reducing the size of the iOS installation package is something that many medium and large apps have to do. Generally, they will start with resource files, compress pictures/audio, and remove unnecessary resources. After optimizing these resources, we can also try to slim down the executable files. The larger the project, the larger the volume occupied by the executable files, and because AppStore encrypts the executable files, the compression rate of the executable files is low. The compressed executable files account for about 80% to 90% of the volume of the entire APP installation package. It’s worth optimizing.
Here are some common optimizations:
TableViewCell reuse
The cellForRowAtIndexPath: callback creates only the instance, returns the cell quickly, and does not bind the data. Bind data (assign) at willDisplayCell: forRowAtIndexPath:.
Highly cache
When the tableView slides, heightForRowAtIndexPath: will be called repeatedly. When the cell height needs to be adaptive, each callback will calculate the height, which will cause the UI to get stuck. To avoid repeating meaningless calculations, cache height is required.
How to cache?
Dictionary, NSCache. UITableView-FDTemplateLayoutCell [if! SupportLineBreakNewLine] [endif] View hierarchy optimization Do not create views dynamically. Cache subViews under memory control. To make good use of hidden. [if! SupportLineBreakNewLine] [endif] Reduce the view hierarchy reduce the number of subviews and draw elements with Layer. Use less clearColor, maskToBounds, shadow effects, etc. [if! SupportLineBreakNewLine] [endif] To reduce unnecessary drawing operation imagesCopy the code
Do not use JPEG images, use PNG images instead.
Child thread pre-decode (Decode), main thread directly render. Because when an image does not have Decode, assigning directly to the imageView does a Decode.
Optimize the image size and try not to zoom dynamically (contentMode).
Combine as many pictures as possible into one for display.
[if! supportLineBreakNewLine] [endif]Copy the code
Reduce transparent View
Using transparent views gives rise to blending, which in iOS graphics processing refers primarily to the calculation of blending pixel colors. The most intuitive example is when we overlay two layers together. If the first layer is transparent, the final pixel color calculation needs to take into account the second layer. This process is called Blending.
Causes of blending:
Alpha UIView < 1.
UIImageView’s image contains an alpha channel (even if UIImageView’s alpha is 1, as long as the image contains a transparent channel, it still causes blending).
[if! supportLineBreakNewLine] [endif]Copy the code
Why does blending cause performance loss?
The reason is straightforward: if a layer is opaque, the system simply displays the color of the layer. If the layer is transparent, it will cause more calculation because the other layer needs to be included in the calculation of the blended color.
Opaque is set to YES to reduce the performance cost because the GPU will not do any composition and will simply copy from this layer.
[if! supportLineBreakNewLine] [endif]Copy the code
Reduce off-screen rendering
Off-screen rendering refers to rendering an image once before drawing it to the current screen.
In OpenGL, the GPU screen can be rendered in the following two ways:
On-Screen
Rendering refers to how the GPU renders in the screen buffer that is currently used for display.
Off-Screen
Rendering refers to when the GPU creates a new buffer outside the current screen buffer to render.
[if! supportLineBreakNewLine] [endif]Copy the code
summary
Performance optimization is not something that can be solved with one or two new releases. It is an ongoing requirement, continuous integration and iterative feedback. In the actual project, the project in the beginning, due to the manpower and time limit, the completion of the project performance optimization of lower priority, such as entered the stage of project put into use, it need to increase the priority, but at the beginning of the project, in the design of architecture, performance optimization point also needs into consideration, early this reflects a programmer technology.
When there is a demand for performance optimization, it usually starts from finding the problem, analyzing the cause and background of the problem, looking for the optimal solution, and finally solving the problem. This is also commonly used in daily work.