[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
Now there are still a lot of businesses who see t...
Mini programs are becoming popular again. Recentl...
How to maintain and increase activity and improve...
Operate the local delivery ordering group project...
I believe that when we surf the Internet, we will...
Today, I was sorting out the articles to be read ...
As Papi Jiang said, the only thing that prevented...
“What’s your prediction? How many thousands will ...
Download Douyin Live Streaming Crash Course Baidu...
Target: A national wedding photography chain clie...
Overview 1. PC optimization methods are also appl...
[[126116]] The Web was originally conceived as a ...
1. Analysis of the national passenger car market ...
We have just launched a plan to upgrade the consu...
Do you have it too? Such experience ↓↓↓ Scalp itc...