The iOS app development series of one day

This paper describes some of my understanding of the iOS development and we hope that app from start to withdraw, will integrate some of the knowledge, forming a chain of knowledge, knowledge related to the current point have runloop, runtime, file storage, interface layout, offline push, memory management, response chain, multi-threading. But most of the more simple and obvious, I try to write their own understanding, professional codes try to paste a link, please correct me if inappropriate.

### 1, click on the icon to begin the work, turn on the main thread, began to run runloop,

 A app, then why can start has survived and respond to user actions, it is because there is a main thread has survived, and a mainline Chengkai Qi runloop. About runloop can be roughly understood as the death of a do-while loop, which is executed over and over again with his task until the process is killed before the program stopped. It assumed the role of a steward of similar small, responsible for handling timer events, listen for messages the user's operating and processing systems.

One o'clock deeper understanding is that there are in fact runloop user mode and kernel mode two states, when there is no need to handle the event, then switch to kernel mode, hibernation starts when an event is received, it switches to the user mode, the end of hibernation . This avoids cpu cycles due to normal while busy waiting, the formation of a "free" wait burden is reduced cpu.

runloop specific process is as follows, it is noted that:
. 1, represents the Timer Event Timers
2, Source0 touch event on behalf of a user, and PerformSelectors
. 3, locks the screen representative of Sourcel / event systems shaking

![runloop](https://upload-images.jianshu.io/upload_images/3288430-7ca2952ad7059140.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Attach great God YYKit blog address: [https://blog.ibireme.com/2015/05/18/runloop/] (https://blog.ibireme.com/2015/05/18/runloop/)


### 2, the main thread to start drawing UI, open information sub-thread to query the local store, get the data back to the main thread interface refresh

At this time, the side began to draw the interface welcome page or start page, while there is no local storage to query the login information. Note that only the main thread to draw UI, so query the login information is in the child thread, this advantage is not blocking the main thread to reduce the waiting time for users.

About multi-threading technology, can be understood as multitasking, just as we can listen to music while on the move, our brains when dealing with walking-related operations, but also to deal with songs related operations.

 iOS where every app has its own little family, we called the sandbox mechanism. All initial small home style is the same, but you can own internal decoration, increase the variety of directory structure.

! [Sandbox directory] (https://upload-images.jianshu.io/upload_images/3288430-5fec4ff437cd3fe7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

It should be noted that each is a separate small house, a guest can not visit a neighbor that can not access or modify data from other app, but you can, for data exchange in the notary system by sharing way. Part of the system-level resources are common, such as albums, cameras, and other contacts, such as direct sunlight is, as a common resource of air.

Sandbox principle on specific encryption mechanism, we can look at this article [article] (https://blog.csdn.net/youshaoduo/article/details/66478551), ah, I did not understand.

iOS in general operation of the local data in several ways, according to the data types can be classified, some pieces of information used by preference, duplication of data such as address book data in large quantities using the database, the database is SQLite iOS, file type data directly file write / read on it.

Then draw the interface, the upper left corner as the origin of coordinates rightward extending downwardly, a normal rendering UI controls, generally need to determine coordinates of the four elements, i.e., X-coordinate (lateral direction), Y coordinate (longitudinal direction), width, height . Using controls interdependent constraints, using the reference principle, the principle is to obtain the coordinates from the set reference condition, for example, the width of the control A is equal to the screen width, the width of the control B is equal to the width of the control A, then we can calculate the controls of B a width of the screen width. This approach Overall, the performance is not as directly coordinate values, but on screen adaptation occupy a lot of advantages, but also very relaxed at the height of the control automatically calculated, reducing the manual calculation steps, it is currently the mainstream program.

! [Coordinates] (https://upload-images.jianshu.io/upload_images/3288430-25347d04c8889436.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


3 ###, user input is received, clicks, and other sliding operation, start processing,

These user's operation, runloop is captured, then the response chain, a step of seeking to control the response, executes a corresponding method. The so-called response chain, that is, from the start screen, descending through all the child controls, if clicked coordinates within the scope of the control, it returns the current control, and continue to traverse the control's child controls until you find the smallest child controls.

This control, if there is a corresponding set method, such as click method, gestures, etc., then according to the method, to find performer method to find the rule is this, first look at the list of methods in the current class, do not go to parent , and then did not go to parent the parent class, until the base class, if there is no base class, entered the message forwarding process.

This means that the message forwarding process, the system developer, I finished layers of checks, are not responding objects of this method, and finally give you a chance, or put forward a method to replace the original method, or give me objects in response to this method. Finally, if the message is forwarded to finish the process, it could not handle, then collapse it. Professional message forwarding analytical look at [here] (https://www.jianshu.com/p/9263720cbd91). And then deep to see the compilation of analytical Xiao Yu the Great God of [the article] (http://yulingtianxia.com/blog/2016/06/15/Objective-C-Message-Sending-and-Forwarding/).

### 4, starts network requests until the data is returned, the main thread returns to get the data to refresh the screen,

Network requests are generally two kinds of get and post requests embodiment, the data is generally not get used to pull the wear parameter, the parameter passed to the background if necessary post is generally used. The reason is that get passed parameters required stitching in the back link request, data security can not be guaranteed, and the length parameter is limited and can not deliver a lot of information.

About http and https we can look at [article] (https://juejin.im/post/5dd50eba6fb9a05a6313ebba).

### 5, 3,4 repeated, intermittent relax, the object is automatically cleaned up unused free memory

iOS OC each object will have a retainCount (reference count) property, when the object is created, the reference count is 1, when other objects held by this object, the reference count plus 1, no longer hold time minus 1, when the object is no longer used by minus 1, if the reference count goes to 0, then the object will be recovered out of the system immediately.

Later, the system provides automatic release of pool-related interfaces, the principle is added to a target in the pool, each runloop to a close, the system makes this all the objects in the pool reference count minus 1, minus 1 if the reference count is 0, then the object is immediately recovered. Note that this situation does not exist reference count becomes negative, citing the pond before if an object is in receipt of a message sent by the system count has become zero, then the object is destroyed immediately, when the system message arrives when the object does not exist, that memory address is empty, while doing nothing to an empty object is no reaction and no influence.

Internal runloop pool has an automatic release when runloop turned on, it will automatically create an auto-release pool, when runloop before the rest can release something autorelease pool, and then re-create a new empty pool is automatically released when runloop when the wake-up lap restart, Timer, Source and other new events will be automatically released into the new pool when runloop exit will be released.

He said memory-related, treatment-related memory leaks naturally, I summed up the situation most of the memory leaks before, see the specific [memory leak analysis iOS Development Series (on)] (https://www.jianshu.com/ [memory leak analysis iOS development series (under)] p / 1b06751130c8) and (https://www.jianshu.com/p/d0008f28053f).

### 6 lunch break, into the background, as there is a process hangs, runloop sleep,

When the user presses the home key, typically about 10s app enters the suspend state, the suspend state is no activity in the app. Special such as playing music, use location based services using voip (point to point audio and video) services, can apply to the system so that app has been running in the background. So some companies are using to play music to make a silent app remain active, be noted that this approach will lead to shelves rejected.

### 7, received a push to remind the user opening the app, the end of the rest, continue to work, runloop paradigm shift,

Push principle is offline iOS by Apple docking background server, app each upload your own device token to the background after the start, when the push needed to send a message, by the background to find the corresponding device token, apple prescribed format, the message and token sent with the Apple server APNs responsible for pushing, and then find the device by Apple server, push messages to show this at the level of the system. Professional full set of processes including background JAVA code parsing see [here] (https://www.cnblogs.com/taintain1984/p/3716642.html).

There is also a place called the push mechanism pushkit, pushkit difference between ordinary APNs is that it will not pop-up notification, but directly to wake up your app, enter a callback, that is, the user can not click on the app start of the case, you run the code we wrote it myself, so operability on most, but because of this push will start on the app, Apple's audit in the user's knowledge is also more stringent.

### 8、

Kill the process, the main thread to stop, to rest from work.

In fact, would like to add something more to go, but at the moment there is no good ideas, comments are welcome.

Welcome to [my nest] (https://zmfflying.github.io/) guest ah, under which records my progress bit by bit, all the grounds of adversity only way forward, and the king of mutual encouragement.

Guess you like

Origin www.cnblogs.com/zmfflying/p/12028321.html
Recommended