runloop small note

First, what is runLoop

  1, it plainly, runloop cycle is running

  2, runloop, he was magic multithreaded

    Generally speaking, a thread can only perform one task, exit after executing thread. However, the main thread can not quit, so we need to let the main thread even if the task is finished, you can continue to wait to receive an event without exiting, then runloop can do.

    But the main thread of non-general, is to perform a task is finished you need to return resources, so the default is not running runloop of.

  3, corresponding to each thread has a runloop, but only by default runloop main thread is opened, runloop other sub-threads is not enabled by default, and programmers need to start manually start.

  

Two, runloop role

  1, to ensure that the program does not quit 

  2, is responsible for monitoring all events (sources, observe, timer) (touch, clock, network events)

  3, runloop very lazy, there is the task of processing, not to sleep

  4, runloop responsible for rendering UI in a loop

 

Three, runloop mode

  NSRunLoopDefaultMode: App的默认Mode,通常主线程是在这个Mode下运行
  UITrackingRunLoopMode: processing mode UI events
  NSRunLoopCommonModes:   UIInitializationRunLoopMode:   GSEventReceiveRunLoopMode: 这是一个占位用的Mode,不是一种真正的Mode(默认在NSRunLoopDefaultMode和UITrackingRunLoopMode这两种模式下
在刚启动App时进入的第一个Mode,启动完成后不再使用
接受系统事件的内部Mode,通常用不到

 

TableView drag when loading a multi-Zhang Gaoqing enlarge, resulting in interface Caton: a scene

  Analysis of a:

    Load multi-Zhang Gaoqing enlarge: need to join runloop, rendering UI when running cycle, enter UITrackingRunLoopMode mode

    Drag tableView: need to add runloop, handle touch events in the run cycle, enter UITrackingRunLoopMode mode

    Load multi-Zhang Gaoqing enlarge, equivalent to runloop cycle rendering the multi-Da map (multiple tasks), taking up most of the time this cycle, leading to interface Caton    

  Solution: Let runloop cycle only render a large image (only do one thing), the definition of the observer, each loop add an image rendering!

  Code, and has achieved great God collated: https://blog.csdn.net/liyanjun201/article/details/79096289

 

 

The following are other great God organize information on runloop very comprehensive   https://blog.csdn.net/qq_30513483/article/details/53373905

What RunLoop is what role, how to get?

  • definition
    • RunLoop essence is an infinite loop, for continuous operation to ensure the program only when the program exits of the time will end (turn the main thread by the main function of RunLoop)
  • effect
    • Continue to keep the program running
    • Handle a variety of events (touch, timer, Selector event) App in
    • Saving CPU resources, improve program performance (doing the work, nothing to do the rest)
  • Acquisition method
    • Using the NSRunLoop (object-oriented), or CFRunLoopRef (bottom C language)

RunLoop principle

  • RunLoop open a cycling event, and accept input events, accept events from two different sources:
    • The input source (input source) (asynchronous transfer events)
    • Timing source (timer source) (transmitting synchronization events)
  • After receiving the message using the four RunLoop method handlePort, customSrc, mySelector and corresponding event processing such timerFired
  • When RunLoop message is not received, it goes to sleep, in order to maintain continuous operation of the program

RunLoop principle

RunLoop receives several input source, the system defines several default mode?

  • There are two input sources
    • Based on the input source port (Port)
    • Custom input source (Custom)
  • The system defined RunLoop There are five modes, there are three of the most commonly used, as follows:
    • NSDefaultRunLoopMode
      • The default mode, the default is the main thread NSDefaultRunLoopMode
    • UITrackingRunLoopMode
      • View scroll mode, RunLoop will be in this mode
    • NSRunLoopCommonModes
      • Mode is not really in the sense, is a placeholder with the "Mode", included by default NSDefaultRunLoopMode and UITrackingRunLoopMode modes

Note the use of the principles and points RunLoop mode?

  • Principles and Precautions
    • RunLoop contains a plurality of Mode, Mode and each comprising a number of Source, Observer, Timer (shown below)
    • Every RunLoop start, you can only specify a Mode, the Mode is called CurrentMode
    • If you need to switch Mode, only to exit the Loop, and re-assign a Mode to enter, so Source between the different groups, Observer, Timer mutual unaffected

RunLoopMode

RunLoop and thread have anything to do

  • RunLoop one correspondence with the thread
  • When the program starts, the main thread to create their own default RunLoop, and set to Default mode
  • When you create a child thread, you must obtain the current thread and start it RunLoop

NSTimer relations and RunLoop of?

  • NSTimer need to add to Runloop in order to perform case
  • NSTimer default is added to the Runloop, the direct execution of

NSTimer accurate, and if not accurate, how to design an accurate timer?

  • Inaccurate
  • Timer should be accurate and consistent with the current thread RunLoopMode

TableView / ScrollView / CollectionView NSTimer Why would stop rolling?

  • A two mode RunLoop not coexist
  • When the scroll view scroll, currently RunLoop in UITrackingRunLoopMode,
  • RunLoopMode inconsistent RunLoopMode NSTimer and the current thread, it will stop
  • Solution: The timer is runloopMode changed UITrackingRunLoopMode or NSRunLoopCommonModes

If NSTimer created in the sub-thread, what will happen, what should pay attention to?

  • NSTimer does not start
    • In the main thread, the system default create and start the main thread runloop
    • In sub-thread, runloop system does not start automatically, manually start
  • Solution:
    • Start sub-thread runLoop 

Download lots of pictures in asynchronous thread, if that fails, how to deal with? Please talk about solutions combine RunLoop

  • RunLoop re-start a network request in an asynchronous thread, download pictures

If starting the program needs to perform a time-consuming operation, how would you do?

  • Asynchronous open a sub-thread and start it RunLoop to perform the time-consuming operation

Runloop relationship with the autoreleasepool

If you start an asynchronous request in the sub-thread, what's the problem?

Determine whether the end of the request, if it is not the end, to maintain the current thread has been started, until the end

When the program starts, runloop How does it work? If starting the program needs to perform a time-consuming operation, how would you do?

When the program starts, the system default create and start the main thread runloop, runloop created by default to listen runloop two Observe and out of sleep and, when he has things to do, nothing Sleep

(Thread (created) -> runloop will enter -> highest priority OB create release pool -> runloop will sleep -> lowest priority OB destruction of the old pool to create a new pool -> runloop will withdraw -> Low priority OB destroy the new pool -> thread (destroy))

Runloop not just create a thread, if you do not take the initiative to get, so there has not been.

Time-consuming operation can be carried out on the sub-thread, after the return to the main thread

Guess you like

Origin www.cnblogs.com/110-913-1025/p/11843882.html