zoukankan      html  css  js  c++  java
  • 初学RunLoop

    RunLoop

    运行循环,跑圈

    可以看出每条线程都有一个与之对应的RunLoop对象

    主线程的RunLoop已经自动创建好了,子线程的RunLoop需要主动创建。

    基本作用:保持程序的持续运行

    处理App中的各种事件(比如触摸事件,定时器事件,Selector事件)

    节省CPU资源,提高程序性能,该做事时做事,该休息时休息。

    相当于

    Bool running = YES;

    do {
    //执行各种任务,处理各种事件

    }while(running);

    return 0;

    如果没有RunLoop,程序就不会卡在那里等你做事情,就会直接运行完自动退出。

    RunLoop对象

    iOS中有两套API来访问和使用RunLoop

    1.Foundation中的

    NSRunLoop

    获取RunLoop对象

    [NSRunLoop currentRunLoop];获得当前线程的RunLoop

    [NSRunLoop mainRunLoop];获得主线程的RunLoop

    2.Core Foundation中的

    CFRunLoopRef

    获得RunLoop对象

    CFRunLoopGetCurrent();

    CFRunLoopGetMain();

    前者是OC的

    后者是C语言的

    而且前者是后者的包装。

    RunLoop资料

    苹果官方文档

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

    RunLoop相关类

    core Foundation中关于RunLoop的5个类

    • CFRunLoopRef
    • CFRunLoopModeRef
    • CFRunLoopSorceRef
    • CFRunLoopTimerRef
    • CFRunLoopObserverRef

  • 相关阅读:
    python 网络爬虫(三)
    python 网络爬虫(二)
    python 网络爬虫(一)
    python 爬虫原理
    (转)栈的生长方向
    CRC校验
    extern关键字
    E
    The Super Powers UVA
    V
  • 原文地址:https://www.cnblogs.com/langji/p/5337963.html
Copyright © 2011-2022 走看看