Check iOS sub-thread operation UI problems, ios thread operation ui
In iOS development, because most functions are not thread-safe, it is very dangerous to operate the UI in the UI sub-thread. However, sometimes the UI cannot be implemented in the sub-thread due to lack of developer experience, you can also know that you did not pay attention to the code writing, or you do not know the functions that operate the UI (such as the reload function of UITableView). This problem often occurs and cannot be prevented, and iOS is not like android, once the UI is operated in the sub-thread, the system crashes directly. In iOS, the UI may only crash, leading to frequent re-occurrence of problems, and frequent crash of stacks is messy, it is a headache to find the location where the error occurred.
I wrote a simple library for Thread Check ui operations. When the sub-thread operates the ui, it will define the crash (VIEW_OP_INSPECT_CRASH) or print the stack (VIEW_OP_INSPECT_LOG_STACK) according to the macro definition (no conflict between the two ), the basic principle is to use the runtime class_replaceMethod to replace most of the uiview APIs (the get operation does not have replace considering performance issues), and then check whether the current thread is the main thread in the replace function.
Github address is https://github.com/pualxiao/PXViewOpInspect (just written a library, constantly improved, Pat. You are welcome to give comments and suggestions and participate in code improvement)
The usage is simple. h and PXViewOpInspect. m can be placed in the project. If a macro in VIEW_OP_INSPECT_CRASH and VIEW_OP_INSPECT_LOG_STACK is defined, the uiview api will be replaced. Otherwise, no.
Considering performance issues, we recommend that you use this library in debug mode. In release, VIEW_OP_INSPECT_CRASH and VIEW_OP_INSPECT_LOG_STACK are not defined.