- Runloop Running Loops
- 1; is a dead loop, the main thread is turned on by default, and the child thread does not open
- 2. Guarantee that the program does not exit
- 3. Monitoring events, application scenarios
- 1. Timer, clock, Nstimer,cadisplaylink
- 2. Monitor the reception and transmission of network port data, socket development
- All event listeners in IOS are all responsible for running loops
- The runloop of the main thread is automatically created when the app is launched
- Other threads need to start themselves under that thread
- You cannot create runloop yourself
- Runloop is not thread-safe, so you need to avoid invoking the current thread's runloop on other threads
- Runloop is responsible for managing autorelease pools
- Runloop is responsible for handling message events, which are input source events, timer events, and network request things
- Runtime run time
- Is the OC object-oriented programming language runtime Environment, Java-like virtual machine
- Runtime is the lower level of OC implementation, the OC code eventually converted to the runtime of the C-language library of Things
- such as data structures such as classes converted to structures in runtime
- The method is converted to a function in the C language, and the calling method is turned into the objc_msgsend () function, which is the message mechanism
- Import Header File <objc/message.h><objc/runtime.h>
- What the runtime can do:
- Get member variables in a class
- Adding member variables dynamically for a class
- Method implementation of dynamically changing class
- Add new methods for class dynamics
- Runtime usage Scenarios:
- 1 classification category, dynamically gets the property array of the class to dynamically add properties to the class
- Category Import Objc,class_copyivar-method-propertylist-protocol, run-time method get class attribute list
- class method written in the taxonomy, + (Nsarray *) cz_objproperties{} get an array of properties for the class
- 1. Establishment of classification methods, class methods
- 2;class_copypropertylist getting an array of class attributes
- 3; Iterate through the array,
- 4,property_getname Get array name
- 5; to OC string nsstringwithcstring: Add to Array
- 6;free (prolist) Note release, Retain/creat/copy
- 2. Dictionary to model, dynamically get the attribute array of class, set the value by KVC
- Category + (instance) cz_objectwithdict{} convert the dictionary of the outside VC into a model object
- 1. Get the list of properties
- 2; Traverse Dictionary enumerate ..., get key,value,
- 3. Determine if key is in an array of attributes [Prolist Containsobject:key]
- If so, use KVC to assign a value, SetValue Forkey:
- 4. Dictionary to model, written in the classification method, the model only attributes, concise
- Drag the classification into, all the objects have this method
- The syntax of all dictionary-to-model frames takes into account the nesting of dictionaries
- 3, to classify, increase the associated object, the development of the framework decoupling
- Increase efficiency by using associated objects dynamically add attributes
- 1;objc_getassociatedobject: Before invoking the run-time method, determines whether the property array is fetched and gets the direct return
- 2. Get an array of properties at run time
- 3;objc_setassociatedobject: Dynamically create attributes, record attribute arrays
- The advantage is that you don't have to call the runtime method every time to improve efficiency
- 4; Using the run-time crossover method swizzling
- 1. Execute your own method
- 2; Then execute the system or third-party framework method
- 3. Black magic, dependent on the System/Framework version, once the change is not good
- AFN crossed the system's resume,suspend,af_resume.
Runloop and Runtime summary