This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.
B: Yes
- Crashes are also a very common problem in iOS development, and even good applications can crash. Such as QQ, wechat and so on.
- It is also a bad experience for users and will increase the maintenance cost
A common crash problem
- An array
- Wild pointer
- Multithreading safety
- KVO
. , etc.
solution
- An array
- If you are an OC developer, you can use method swaps to check whether the boundary is crossed (if it is a dictionary, the key exists) in the call function
- Or each time you use a value to determine whether it exists
- Wild pointer
- The most common method in oc is to determine whether an object is nil before each call in
- Or in the OC, add a GET method to each attribute via the Runtime attribute
- This rarely happens in Swift, which is a strongly typed language and a powerful optional mechanism, but does not execute data on objects that do not exist
- Multithreading safety
- When using threads, pay attention to multithreading issues, avoid zombie objects, operate on one object at a time, remember to lock
try catch
Try catch is used to capture crashes
- Try is the object that needs to catch the exception
- To throw an exception
- Finally means that after an exception occurs, execute the following code
NSArray *arr = [[NSArray alloc] init];
@try {
NSLog(@ "% @", arr[0]);
} @catch (NSException *exception) {
NSLog(@ "% @", exception);
} @finally {
NSLog(@"finally");
}
Copy the code
It feels like most iOS developers use try catches very sparingly, and are less aware of exceptions than the backend. The system gives us great extensions that we need to use more
Third-party tools
-
For example, you can try to access some third-party tools bugly, Bugtags, etc
-
Analyze the crash information sent to third-party tools to determine the crash cause
-
After finishing the package, you should leave the package file, which is a dSYM file, you can find the corresponding symbol table information
Note that the build version will automatically generate dSYM files, but the debug is not available, we need to manually enable. Search debug in Build Setting and change the following two items to correct Settings
Finally, according to the documents of Bugly, to parse the crash file of the platform
Finally find the file and method of crash
Second, caton
Common problems with APP lag
- File loading is too large
- Blocking main thread
- Apply colours to a drawing
. , etc.
solution
- Most of the time-consuming operations, try to do child threads
- Finally update the UI on the main thread
- Be aware of thread safety when working with multiple threads
One way to use a large Cache is to Cache data
- Optimization of rendering:
- Off-screen rendering: An off-screen rendering is triggered when complex operations are performed on layer
- shouldRasterize
- Masks
- Shadows
- Edge Antialiasing
- Opacity: group opacity
- If you can manipulate view, try not to manipulate layer
table View
To optimize the
- Avoid using complex cells and use hidden to hide them, minimizing the need to add or subtract views during reuse
- Minimize the use of automatic height calculation, self-adaptation of label, etc. (For complex cells, try to calculate in advance)
- Reduce xiB/SB cell layout (this effect is actually small)
- Reduce cell operations, layouts, and so on during scrolling
- Optimize data display and refresh of cells according to scrolling status and display of cells to reduce excessive consumption of performance of undisplayed cells
- Be sure to check that UI updates are working properly and that threads are not blocked by other asynchronous operations, such as networks
other
You can use xcode’s built-in tool Instrument to detect memory, elapsed time, zombie processes, and so on
You can also use Tests to see how long it takes to test a function
Three, power consumption,
Select the app to be tested in the Xcode instrument and select Energy Log to view the power consumption of the APP
You can directly analyze the power consumption of the network, Wi-Fi, and CPU
Can also be combined with the kadon to analyze, the scene of the death of the kadon is also the most power consumption.