GCD and Design Patterns

Source: Internet
Author: User

Multithreaded programming

In IOS programming, there are several situations in which you need to deal with Multithreading: UI events must be done on the mainline thread, others can be carried out in the background, and tasks that are time-consuming or blocking threads are finally placed in the background line thread. There are several multithreading techniques for IOS: threading, GCD, and Nsoperationqueue. Threading this technique is more complex, and multithreaded programming has always been "complex must die", it is recommended to use both, but the thread has a later advantage: the ability to accurately guarantee the time of task execution. GCD full name is the Grand Central Dispatch, is libdispatch the external code of this library, based on the bottom of C to implement, and Nsoperationqueue, commonly known as the Operation queue, is based on GCD implementation. GCD can do nsoperationqueue basically can do, and there are some GCD features that are not easy to implement, such as suspend, cancel the task, although in IOS 8, GCD also provides the ability to cancel the task, but in the GCD of the task hangs and cancellations have a greater limitation; though most of the cases Should use a higher level of abstraction API, that is, nsoperationqueue, but to handle the general background tasks I prefer GCD, mainly GCD collocation blcok easy to use, very convenient. How to choose, the following two links a discussion of this issue is worth seeing:

Stackoverflow:nsoperation vs. Grand Central Dispatch

Blog:when to use Nsoperation vs. GCD

In addition, these articles are recommended: OBJC's concurrent programming topic "Concurrent Programming" and the Chinese translation version; Lei Chunfeng's Blog "Operation Queues for IOS concurrent programming"; Nshipster "Nsoperation".

Design Patterns

Evaluate the advantages and disadvantages of several design patterns of Delegate, Notification and KVO

I do not think this problem is a good question, compared with the advantages and disadvantages of these design patterns, it is better to talk about their respective characteristics, because they are designed to solve a certain type of problem, have their own appropriate use of the scene. Also, give an introduction to designing patterns in iOS: iOS design Patterns.

Why do the topics like to compare the three design patterns? Both Notification and KVO are used to assist in communication between objects: An object listens for an event, and when an event occurs, the object is notified and then responds. These words are probably in the books that have been read before. If you have not been exposed to design patterns before, you will always be able to see events and respond to such vague words for the first time, and see you foggy, OK, I'm talking about me. But delegate, it should be said that there is no monitoring function, but when the event occurs or the time has come, ask delegate object to do something. When I first started to learn OC, a book likened delegate to an assistant, who did not understand it at the time, and now found the metaphor very appropriate. Although the delegate pattern is ubiquitous in OC, it is widely present in the Uiviewcontroller class, but in the process of developing facealbum, only one custom protocol/delegate is encountered and later replaced with KVO. In contrast to Notification and KVO modes, using delegate mode you will know exactly what the object's delegate can do, because to be a delegate of an object, the object has to obey the specified Protocol,protocol specified delega The method that the Te object needs to implement.

Notification and Kvo both need to listen to the object of the event (early to see the event is dizzy, now write to feel that the word is very handy) to register, delegate need to delegate object to comply with the specified protocol;notification In the listener to a singleton object Nsnotificationcenter registration, nsnotificationcenter similar to a broadcast center, accept any object registration, the latter to listen to the object to register, one-to-one, both do not need to have a link between the object, and Delegate the object that needs to communicate through the variable connection, the Listener object in Nsnotification mode communicates with the object being monitored by nsnotificationcenter this intermediary, and KVO, can not say that the two are direct communication, I have not understood KVO is how to achieve communication, from the surface of the two are so telepathic, this is the system for us, and delegate, because through the variable connection, directly to the delegate send messages can, in this point, nsnotification do not need to communicate the two sides know each other, The latter is not the case; In response to an event, both the nsnotification and KVO modes Specify the response method at the time of registration, while delegate pre-defines the response method in protocol.

Said so much, not intuitive, say a real scene, such as in the Uicollectionview to select the cell, I hope that the title can track the number of selected cells. Here with Nsnotification and KVO can be achieved, but I prefer KVO, feel more elegant, because the use of Nsnotification mode, select a cell to manually publish the notification in the selected method, and KVO, as long as the observed property implementation KVO Compatible method would be possible; and delegate, himself to do his own delegate, uh. In the face of some system events, such as the appearance and disappearance of the keyboard, picture library changes, the use of nsnotification more natural, because KVO is limited to the object property tracking.

To write so much for the time being, it is recommended that when you use the delegation, Notification, or observation in IOS, you may need to FQ.

GCD and Design Patterns

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.