[Introduction] The APIs of the iOS 10 push part use the naming method of CompletionHandler extensively. In this article, we will compare the particularity of this Block to better understand and practice the CompletionHandler style Block in your own projects. text When we integrate a Lib (also called a wheel, SDK, and Lib in the following text), as a developer, we will find that the Blocks we encounter can be divided into the following types according to their functions:
The difference between these two places: the former is "Block execution" and the latter is "Block filling". Apple has not clearly pointed out the difference in naming and functions between Callback and CompletionHandler in its coding standards. However, if we divide the functions into "execution and filling", the naming of callback and completionHandler can be treated separately. It is also convenient for the caller to understand the function of the block. But in general, in Apple's official naming, the "Block filling" function is generally named "completionHandler", and the "Block execution" function is mostly named "callback", and a small number of them are named "completionHandler". for example: In NSURLSession, the following function names the "callback" as "completionHandler":
We often see CompletionHandler being used in the first scenario, while the first scenario "Block execution" is more appropriately named Callback. Not all blocks are suitable to be called CompletionHandler In general, the design of CompletionHandler often takes multi-threaded operations into consideration, so you can operate asynchronously and then execute the CompletionHandler at the end of the thread. The following example will describe some advantages of the CompletionHandler method in multi-threaded scenarios. CompletionHandler + Delegate combination This type of Block is widely used in the newly added UserNotificaitons in iOS10, for example:
The documentation for completionHandler is as follows:
The same applies here:
There is another very common example (one of the four essential proxy functions when using URLSession in Delegate mode)
In the proxy method implementation code, if completionHandler(NSURLSessionResponseAllow) is not executed, the http request is terminated. CompletionHandler + Block combination A function that uses a function as a parameter or return value is called a higher-order function. According to this definition, Block takes Block as a parameter, which is a high-order function. Let's take a look at an example based on a practical application scenario: If there is such a requirement: Take my previous IM project ChatKit-OC (open source, referred to as ChatKit below) as an example. When your application wants to integrate an IM service, your APP may have been launched at this time, and it already has its own registration and login processes. Chatting with ChatKit is very simple. You only need to give ChatKit an id. The chat is normal, but both parties can only see one id, which is a bad experience. But how to display avatars and nicknames? So I designed such an interface, -setFetchProfilesBlock:. This is the block where the upper layer (APP) provides user information. Since ChatKit does not care about business logic information, such as user nicknames, user avatars, etc., users can inject a user information content provider block into ChatKit through the ChatKit singleton. Only through this user information provider block can ChatKit correctly draw business logic data. The schematic diagram is as follows: The specific implementation is as follows: The method definition is as follows:
The usage is as follows:
For the application scenario of the above Fetch method, the return value of the actual method can also be realized, but compared with CompletionHandler, the inability to switch threads freely is a disadvantage. |
<<: iOS Auto Layout Framework – Masonry Detailed Explanation
>>: Android keyboard panel conflict layout flashing solution
Recently, in the World Cup Portugal beat Ghana 3-...
Today, I will introduce to you some tips on how t...
Facebook video ads are the most intuitive and eff...
"Slow down on your blind date, you should go...
199it News July 5, 2017, is perhaps the most impo...
The Pinduoduo platform is relatively easy to oper...
In the private domain operation capability model,...
Two mobile phones and two internet celebrities tr...
...
On November 8, the media reported that Xiaohongsh...
Chuangqi Academy · Knowledge Payment Annual Incom...
Is it easy to join the Zibo Second-hand Car Mini ...
Today I’m sharing a real case of a client who spe...
The popularity of smartphones, coupled with the r...