Reactiveswift Source Parsing (10) Lifetime code implementation

Source: Internet
Author: User
Tags call back closure

In order to post the blog, this blog we will talk about the Reactiveswift framework of the Lifetime class concrete implementation. From the name of lifetime, we are in this way, that is the life cycle. Using Lifetime in Reactiveswift to mark the life cycle of an object, the main function is to call back the object's deinit () destructor by sending a semaphore. Next, let's look at the implementation of the Lifetime class. The lifetime class, similar to event and Observer , is also a more atomic class that exists in the form of atomic components in Reactiveswift.

Below we will first give an example of the use of Lifetime, and then, based on the output of the example, look at the specific code implementation of Lifetime and how it works.

I. Examples of lifetime use

For the characteristics of Lifetime , we give the example below. Of course the reactiveswift online is not a single example of Lifetime , because Lifetime is not a separate external service. Below is an example of what we write about the Lifetime class.

1, Lifetime () method implementation

First, let's look at the lifetime () method below: First, a Lifetime object and token corresponding to the object are created through the Make () factory method in the Lifetime class. and the Lifetime () method calls, requires a reference parameter Tokenref, that is, Tokenref is the inout type of parameter, through the tokenref parameter can be The token returned by Make () is passed to the outside of the function.

Then use the observeended () method of the Lifetime object to add two observers. The trailing closure that follows is the closure block that is executed when token is released. Because the Lifetime object is used in addition to the lifetime () method, there is no reference anywhere else, depending on the characteristics of the strong type in arc, the Lifetime object is freed after the lifetime () method call ends. So we give the log "lifetime will be released " at the end of the lifetime () function.

The token object in the lifetime () method is used by a scope other than the method by tokenref this inout type parameter, so the heap space corresponding to token is not freed after the lifetime () method call ends.

2. Invocation of the Lifetime () method

In the Taplifetimebutton () method, we call the Lifetime () method described above. First we declare a tokenref variable of type any, which is passed as a parameter to the lifetime () method when the lifetime () function is called. That is, the tokenref variable refers to the heap space corresponding to the token object in the Lifetime () method.

When Lifetime () is called, the heap space corresponding to the Lifetime object in lifetime () is only referenced in the scope of the lifetime (), so when the method finishes executing, the The heap space corresponding to the lifetime is immediately released.

When Tokenref is set to nil, the heap space corresponding to token will be released immediately. When token is freed to perform token-corresponding destructors, the destructor is sent to those observers who are added through the Lifetime Observeend () method. So when token is released, it executes the trailing closure of the Observeend () method.

  

3. Operation Result

Below is the result of the above example, we can compare the above code implementation according to the output result below. It is easy to know from the output that the Lifetime object was released after the Lifetime () method call was executed. The heap space corresponding to the token object allocated in the lifetime () method is released when Tokenref is set to nil, and the message is sent as an observer who observes the life cycle of the lifetime before releasing it.

Judging from the running results, thelifetime of this life cycle is essentially a token to mark the life cycle of an object, and the release of the lifetime object is not directly related . Later, when we talk about the token class and the Lifetime class, we will be at a glance.

  

Second, the internal class token in lifetime

After reading the lifetime example, let's take a look at the internal code implementation of lifetime. A token class is defined inside the lifetime class. The token class object is used in the lifetime, and the token usage is given later. Let's take a look at the code implementation of the token class inside the Lifetime class.

Token class implementation is relatively simple, a short summary of the function of token: which used the signal pipe method to create a ended semaphore, and get to the ended semaphore send event Endedobserver, The complete event is then sent using the Endedobserver sendcompleted () method in the Deinit destructor . This statement sums up all the functions in token.

Below is the code implementation of the token class, one of the things we need to note is that the type of value that is sent in Token's ended semaphore is a non-parametric closure . In the later content, when used in the introduction.

  

Ended properties and constructors for lifetime

After talking about token's code implementation, let's talk about the object properties and constructors in lifetime. There is only one object property in the Lifetime class, and that is the ended semaphore. The type of semaphore is also a value that can send a closure with no parameters and no return value. the main constructor of lifetime is to assign values to ended . The specific code is shown below.

  

Iv. convenient constructors, factory methods, and empty static properties for lifetime

1, lifetime of the convenience of the construction device

Let's take a look at Lifetime's handy constructor. The convenience keyword in the code snippet below is decorated with lifetime's handy constructor. The parameter of the convenience constructor is a token type object, and the Lifetime constructor is called in the convenience constructor, and the ended semaphore of the token object is passed to the Lifetime constructor. So the ended signal in lifetime is actually the ended semaphore in the token object.

2, Lifetime's Factory method

After talking about Lifetime's handy constructor, let's talk about Lifetime's factory approach. In our previous blog, design Patterns (iv): Exploring simple factories, factory methods, and abstract factory patterns from the Arsenal are a detailed introduction to the factory model. The role of the Make () static method in the code snippet below in the Lifetime class is the factory method, which is responsible for creating objects of the Lifetime class. There are two main things in the Make () method, one is to instantiate a token object token, and then pass the token to Lifetime's handy constructor. Finally, the objects of the lifetime class created by token and the convenience constructor of lifetime are returned in the form of tuples. The specific code is shown below.

3. Empty static computed properties of lifetime

The empty static computed attribute class of lifetime is similar to the empty static computed property of signal. From its implementation code we can see that the ended semaphore in the lifetime object created by empty is a signal.empty type of semaphore. That is, the Lifetime object created by empty is a life cycle that has ended. The specific code is as follows.

  

V. observeended () method implementation

Now let's take a look at the code implementation of the observeended () method. The main purpose of this method is to add the observer to the bag container in the ended semaphore. When an observer receives an event that is isterminating, the trailing closure provided by the observeended () method is executed.

And this endend semaphore is essentially the endend semaphore in the token object. When the token object is refactored, the endendobserver sendcomplete () method of the EndEnd semaphore sends the signal is called in the token's destructor to send the complete event, This is what the above code does in the Deinit method of the token class.

  

VI. Lifetime Executive schematic

Below is a diagram of the execution between Lifetime, tokens, Signal, and Observer . In the Lifetime class, token plays a vital role. Lifetime's working principle is actually the life cycle of an object that uses tokens to represent the life cycle of an object. If the object space of the Lifetime is freed, but the heap space corresponding to the token object exists, then the life cycle of the object corresponding to Lifetime is still continuing, as can be seen from the code example above.

Or that sentence, essentially using the token object's life cycle to represent the lifetime of an object.

  

Today's blog will be here first, the next blog we will continue to parse the reactiveswift framework of other content.

The above code GitHub share address:https://github.com/lizelu/TipSwiftForRac .

Reactiveswift Source Parsing (10) Lifetime code implementation

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.