"Effective OBJECTIVE-C 2.0" after reading summary of four

Source: Internet
Author: User
Tags gcd

Written in the previous words:

1, from now, this moment, began to work hard, move up!

2, "Effective OBJECTIVE-C 2.0" This is a very real book, each chapter with specific examples to inform readers of such reasons!

3, the book has been bought for a long time, but also go out to see, this is in accordance with the book chapters, summed up the contents of the book and their own experience!

4, willing to share the same!

37th: Understanding the concept of "block"

37.1: Block is the syntax closure in C, C + +, objectivve-c;

37.2: Blocks can accept parameters, or return values;

37.3: Blocks can be allocated on stacks or heaps, or they can be global. The blocks allocated on the stack can be copied into the heap, which, like standard deobjective-c objects, has a reference count!

38th: Creating a typedef for common block types

38.1: Redefine the block type with TypeDef, it is easier to use another block variable;

38.2: The new type should be defined in accordance with the existing naming conventions, do not make their names and other types of conflict;

38.3: You might want to define more than one type alias for the same block signature. If the code you are refactoring uses an alias of the block type, you only need to modify the block signature in the response typedef without altering the other rypedef.

39th: Use handler block to reduce the degree of code dispersion

39.1: When creating an object, you can use the inline handler block to declare the relevant business logic together;

39.1: When there are multiple instance objects need to monitor, if the use of delegate mode, then often need to according to the incoming object to switch, and if you use handler block to implement, you can directly put the block and related objects together;

39.2: If the handler block is used when the API is involved, you can add a parameter that allows the caller to decide which queue the block should be scheduled to execute on.

40th: Do not show a retention ring when referencing objects with blocks

40.1: If the object captured by the block directly or indirectly retains the block itself, then be careful to keep the problem;

40.2: Be sure to find the right time to remove the retention ring, instead of pushing the responsibility to the caller of the API.

41st: Multi-use dispatch queue, less use of synchronous lock

41.1: The dispatch queue can be used to express the synchronization semantics (synchronization semantic), which is simpler than using @sychornized blocks or Nslock objects;

41.2: Combining synchronization with asynchronous dispatch, you can achieve the same synchronization behavior as the normal locking mechanism, but do not block the execution of asynchronous dispatch threads;

41.3: Use synchronous queue and fence block, can make synchronization behavior more efficient!

42nd: Multi-use GCD, less use Performselector series method

The 42.1:performselector series approach is prone to lapses in memory management. It cannot determine what the selector is to be executed, so the arc compiler will not be able to insert the memory management method used;

42.2:performselector series methods can deal with the selection is too limited, the return value type of the selection and the number of parameters issued to the method is limited;

42.3: If you want to put the task on another thread to execute, then it is better not to use the Performselector series method, but the task should be encapsulated in the block, and then call the large central distribution mechanism of the relevant methods to achieve.

43rd: Master GCD and operation of the use of the queue time

43.1: The dispatch queue is not the only solution when troubleshooting multithreading and task management issues;

43.2: Operation Queue provides a set of high-level objective-c API, can achieve the most functions of pure gcd, but also can complete some more complex operations, those operations if you use GCD to implement, you need to write another code!

44th: Through the dispatch group mechanism, according to the status of system resources to perform tasks

44.1: A series of tasks can be grouped into one dispatch group. Developers can be notified when this set of tasks is completed.

44.2: Through the Disaptch group, you can perform multiple tasks simultaneously in a concurrent dispatch queue. At this point GCD will dispatch these concurrently executed tasks according to the system resource status. Developers need to write a lot of code if they want to implement this feature themselves!

45th: Use Dispatc_once to execute the thread-safe code that runs once in order

45.1: It is often necessary to write "thread-safe code that only executes once" (Thread-safe single-code execution). With the Diapstch_once function provided by GCD, it is easy to implement this function;

45.2: The tag should be declared in the static or Gloabal scope, so that the tokens that are passed in are the same when you pass the block that only executes once to the Dispatch_once function.

46th: Do not use Dispatch_get_current_queue

The behavior of the 46.1:dispatch_get_current_queue function is often different from what the developer expected. This function has been deprecated and can only be used for debugging purposes.

46.2: Because the dispatch queue is organized hierarchically, it is not possible to describe the concept of "current queue" with a single queue object alone.

The 46.3:dispatch_get_current_queue function is used to resolve deadlocks caused by non-reentrant code, but problems that can be solved with this function can often be resolved using "queue-specific data" instead!

This chapter is mainly a block and large central distribution, can be said to be the current OC into the Foundation and core! This part of the relevant examples used in the development of the project to further improve and improve!!!

Write at the end of the words: every day to try a little bit, perhaps not obvious, but eventually will help!

Very embarrassed, the whole September did not update! Willing to continue to work!

"Effective OBJECTIVE-C 2.0" after reading summary of four

Related Article

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.