What are the common memory loop references?

• NSTimer:

NSTimer is often used as a member variable of a class, and NSTimer is initialized to specify self as target, which is easy to create a circular reference (self->timer->self). In addition, if the timer is always in the validate state, the reference count will always be greater than 0, so you should call the invalidate method first when the timer is no longer in use. In other words, you must remove the timer.

• Block usage:

A block copy either makes a strong reference (ARC) or increases the retainCount by 1(non-ARC) for objects used within the block. Improper use of blocks in both ARC and non-ARC environments can cause circular reference problems. This typically occurs when a class uses a block as its property variable and then uses the class itself in the block’s method body. Self. SomeBlock = Typevar{[selfdosomething]; Or self.othervar = XXX; Or _otherVar =… }; The reason for the loop is: self->block->self or self->block->_ivar

Delegate:

Assign (MRC) or weak(ARC) when declaring a delegate, please assign(MRC) or weak(ARC).

2.MRC and ARC memory management mechanism

Under MRC, OC memory management follows the mechanism of “who creates, who releases, who references, and who manages”. When an object is created or referenced, an ALLOc, copy, and retain message is sent to it. When the object is released, a release message is sent.

ARC is automatic reference counting, and the administration mechanism is the same as manual except that instead of calling retain, Release, autorelease, it inserts release and autorelease in place. Using ARC does not eliminate the need for memory management, such as avoiding circular references when using blocks and using weak Pointers when using proxies as properties.

3. The RunLoop understanding

Each thread has a message loop — > the message loop listens for input events — > Events have two types of input sources and timing sources — > Add the created input source to the message loop in a certain mode — > Since the child thread message loop is disabled by default, Therefore, the thread cannot listen to whether there is a method that needs to be executed, so it will be destroyed, resulting in the method cannot be executed – > need to start the message loop of the child thread – > three open methods, run can not be closed, runUntilDate is not available even though the specified time is enabled. Apple provides judgment mode. Click the help bar to the right of Run to find it. RunLoop is a time loop that listens for events and keeps the application running. When an event is heard, a message is sent to the registered object to implement the event response

The purpose of a Runloop:

It is responsible for handling input events (event types are as follows :). If no event occurs, the program will go to sleep

In layman’s terms, runloop is used to ensure that the program is not terminated by the system during execution and that user interaction is constantly monitored

Runloop runs in loop mode:

There are four modes. The first is the default mode: the mode used for normal method calls. The second is trace mode: used for scrollView to track touch slides so that they can coexist without being affected by other modes. The third is the run cycle mode that is first turned on when the program starts. The fourth one is unheard of in development. I haven’t studied it. There is also a placeholder mode. But it’s not much use.

Runloop nature:

It’s actually a structure. It has obserner in it. Nstimer. Source (listening for events), etc

Use scenarios for proxy, observer, singleton design patterns?

Singleton design pattern:

Ensure that only one instance exists for a given class and that this instance has globally unique access points. For example, NSUserDefaults, UIApplication, NSFileManager, and encapsulating network utility classes.

Agent design pattern:

Usually there is a one-to-one relationship between sender and receiver, with the goal of changing or passing the chain of control, allowing one class to notify other classes at certain moments without needing to retrieve Pointers to other classes. Use proxies a lot in UIKit.

Both KVO and Notification are observer modes:

Any change in the state of an object notifies other objects that are observing it. Notification is often used for a bunch of multiple values. It is often used when there is no correlation between two objects. For example, when the keyboard is popped or hidden, Notification is used. KVO is the key-value observer mode, which automatically notifies the corresponding observer when the properties of a specified object are modified.