IOS Development Note (ix)

Source: Internet
Author: User
Tags gcd

1,GCD's concurrent, Serial queues

Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks is St Ill started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that is managed by the dispatch queue. The exact number of the tasks executing at any given point are variable and depends on system conditions.

Concurrent queues are used to perform one or more tasks concurrently, but the tasks are also started in the order in which they were added to the queue. Tasks that are performed on different threads are managed by concurrent queues. The number of concurrently executed tasks is dynamic, depending on the conditions of the system.  

Serial queues (also known as private dispatch queues) execute one task at a time of the order in which they is added to T He queue. The currently executing task runs on a distinct thread (which can vary from task to task) that's managed by the Dispatch Queue. Serial queues perform tasks in the order in which they are added. The kernel creates additional threads when workunits on existing GCD worker threads for a global concurrent queue is bloc Ked in the kernel for a significant amount of time (as long as there are further work pending on the global queue). When the concurrency queue When the task is blocked for too long, the system creates a new thread to perform the subsequent tasks because there are many more tasks waiting behind. Dispatch_barrier_async barrier, waiting for the completion of the front. Dispatch_semaphore_wait simply means that the GCD asynchronous queue is executed as soon as possible, so if a block is blocked, a new thread is created. If you do not want to have too many threads, you can use the synchronization queue,

Concurrent queues and main queue are generated by the system and Dispatch_suspend, Dispatch_resume, Dispatch_set_context, these functions are invalid for them

2, inter-thread communication

Mach Port
Configuring a port-based Input Source in the Run pool section of Apple's thread Programming Guide has an example of using Mach Port for inter-thread communication. The essence of this is that the parent thread creates a Nsmachport object that is passed to the child thread as a parameter when the child thread is created, so that the thread can send a message to the passed-in Nsmachport object, and if it wants the parent thread to send a message to the child thread, Then the child thread can send a special message to the parent thread, passing in another Nsmachport object that it created, so that the parent thread holds the port object created by the child thread and can send a message to the Port object of the child thread.

Of course, the respective port object needs to set delegate and Schdule into the runloop of their thread, so that after the message, the delegate method that handles the port message is called, and you can process the message yourself. 3,RUN LOOP

If you have used a select system call to write a program you can quickly understand the concept of Runloop event source, essentially the mechanism of event source and select is a multiplexed IO implementation, in a thread we need to do is not a single thing, such as the need to handle the fixed clock events, Need to handle the user's touch event, need to accept data from the remote end of the network, the need to do all the things to register to the event source, each time the beginning of the cycle to check whether the source of these events need to process the data, and some to deal with. Take a specific application for example, nsurlconnection network data request, the default is asynchronous way, its implementation principle is to add it as an event source after the creation of the current runloop, while waiting for the network response and network data acceptance of the process is in a newly created independent thread to complete, When the thread processes to a stage, such as getting a response from the other party or after accepting the network data, it notifies the previous thread to execute its associated delegate method. So in cocoa often see scheduleinrunloop:formode: This way, this is to add it to the event source, when an event occurs, the relevant delegate method is called. For the corefoundation layer, the usual pattern is to create the input source, and then add the input source through the Cfrunloopaddsource function to the Runloop, and the related callback function is called after the related event occurs. such as the use of cfsocket. In addition, there is a concept of running mode in Runloop, each running cycle must run in a certain mode, while the pattern exists in order to filter the event source and observer, only those event sources and observers that are consistent with the current Runloop run mode will be activated.

Each thread has its corresponding runloop, but the default non-main thread of Runloop is not running, you need to add at least one event source for Runloop, and then go to run it.  In general, it is not necessary for us to enable thread runloop unless you have a long time to detect an event in a separate thread. 4,view hierarchy Viewdidload This method is called after the view is AddsubwiewViewdidload is used to initialize and load. (Whether you load a view from xib or generate a view from Loadview, it is called.) ) Loadview This method is called when the controller's view is nil. This method is used when creating a view programmatically. Loadview is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. when the device is out of memory, the view controller receives a didreceivememorywarning message. The default implementation is to check whether the current controller's view is in use. If its view is not inside the view hierarchy that is currently in use, and your controller implements the Loadview method, then this view will be release, and the Loadview method will be re-used to create a new view.  View hierarchy is also an important part of responder chain, and when we need to render the contents of a window, the framework of the application uses it to detect the layers of views, to determine the parts that need to be rendered, so as to avoid doing some useless work, This is crucial.  

At the time of development, sometimes you will encounter

1.ios attempt to present whose view isn't in the window hierarchy

2.warning:attempt to present on whose view isn't in the window hierarchy!

such as such a hint, as long as the inside hint window hierarchy, are the view hierarchy understanding is not in place caused.

The above problems are in a controller's view has not been added to the window time and take present another controller, which is equivalent to the building, 2 floor has not finished, directly to cover the 3 floor, so certainly not.

The most straightforward solution to the problem above is to call present in the controller's viewdidappear. This ensures that the hierarchical structure of the view hierarchy is not chaotic.

IOS Development Note (ix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.