NSRunloop简单细说(二)—— 获取运行循环及其模式

版本记录

版本号 时间
V1.0 2017.08.22

前言

NSRunloopOC Foundation框架中非常重要的一个类,很多时候我们会使用它,但是未必对其有深入的了解,接下来几篇我就会带着大家重新学习一下NSRunloop这个类,从简单到复杂,从基本到深化,我会一步步的走完。希望对大家有所帮助。感兴趣的可以看我上一篇。
1. NSRunloop简单细说(一)—— 整体了解

一、 @property(class, readonly, strong) NSRunLoop *currentRunLoop;

该属性的作用是:获取当前线程的runloop模式,是一个只读属性。这里还要注意:

  • 如果线程不存在运行循环,则创建并返回一个。

下面看一下代码。

- (void)demoCurrentRunLoop
{
    NSLog(@"currentRunLoop1 = %@", [NSRunLoop currentRunLoop]);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"currentRunLoop2 = %@", [NSRunLoop currentRunLoop]);
    });
}

下面我们看输出,这个输出还是很长的。

currentRunLoop1

2017-08-22 23:26:46.152 JJOC[3275:96954] currentRunLoop1 = {wakeup port = 0x2103, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = {type = mutable set, count = 2,
entries =>
    0 : {contents = "UITrackingRunLoopMode"}
    2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = {type = mutable set, count = 16,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    1 : {valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}
    2 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}
    3 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : {signalled = No, valid = Yes, order = 0, context =  {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    5 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    7 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
    9 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = }
    10 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = }
    11 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
    12 : {signalled = No, valid = Yes, order = 0, context =  {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    13 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = }
    14 : {signalled = No, valid = Yes, order = 0, context =  {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    18 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = }}
    21 : {valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = }
    22 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = }}
}
,
modes = {type = mutable set, count = 5,
entries =>
    2 : {name = UITrackingRunLoopMode, port set = 0x2703, queue = 0x600000175c00, source = 0x6000001d1580 (not fired), timer port = 0x2903, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    3 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    5 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    1 : {signalled = No, valid = Yes, order = 0, context =  {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    3 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = }}
    4 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = }}
    5 : {signalled = No, valid = Yes, order = 0, context =  {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    6 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = (null),
    currently 525108406 (10580007687927) / soft deadline in: 1.84467335e+10 sec (@ -1) / hard deadline in: 1.84467335e+10 sec (@ -1)
},

    3 : {name = GSEventReceiveRunLoopMode, port set = 0x2f03, queue = 0x608000175540, source = 0x6080001d2570 (not fired), timer port = 0x3103, 
    sources0 = {type = mutable set, count = 1,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
}
,
    sources1 = {type = mutable set, count = 1,
entries =>
    1 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
}
,
    observers = (null),
    timers = (null),
    currently 525108406 (10580009074827) / soft deadline in: 1.84467335e+10 sec (@ -1) / hard deadline in: 1.84467335e+10 sec (@ -1)
},

    4 : {name = kCFRunLoopDefaultMode, port set = 0x2203, queue = 0x600000175840, source = 0x6000001d13a0 (not fired), timer port = 0x2403, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    3 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    5 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    1 : {signalled = No, valid = Yes, order = 0, context =  {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    3 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = }}
    4 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = }}
    5 : {signalled = No, valid = Yes, order = 0, context =  {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    6 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = {type = mutable-small, count = 1, values = (
    0 : {valid = Yes, firing = No, interval = 0, tolerance = 0, next fire date = 525108408 (1.41212302 @ 10581423929411), callout = (Delayed Perform) UIApplication _accessibilitySetUpQuickSpeak (0x1047d118f / 0x1073e6c55) (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/UIKit.framework/UIKit), context = }
)},
    currently 525108406 (10580009118217) / soft deadline in: 1.41481117 sec (@ 10581423929411) / hard deadline in: 1.41481113 sec (@ 10581423929411)
},

    5 : {name = UIInitializationRunLoopMode, port set = 0x4523, queue = 0x6000001765c0, source = 0x6000001d1a30 (not fired), timer port = 0x4723, 
    sources0 = {type = mutable set, count = 1,
entries =>
    2 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
}
,
    sources1 = {type = mutable set, count = 0,
entries =>
}
,
    observers = (
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = }"
),
    timers = (null),
    currently 525108406 (10580011879022) / soft deadline in: 1.84467335e+10 sec (@ -1) / hard deadline in: 1.84467335e+10 sec (@ -1)
},

    6 : {name = kCFRunLoopCommonModes, port set = 0x6007, queue = 0x600000176380, source = 0x6000001d1c10 (not fired), timer port = 0x6203, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = (null),
    currently 525108406 (10580012030882) / soft deadline in: 1.84467335e+10 sec (@ -1) / hard deadline in: 1.84467335e+10 sec (@ -1)
},

}
}

currentRunLoop2

2017-08-22 23:26:46.155 JJOC[3275:97472] currentRunLoop2 = {wakeup port = 0x7303, stopped = false, ignoreWakeUps = true, 
current mode = (none),
common modes = {type = mutable set, count = 1,
entries =>
    2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = {type = mutable set, count = 1,
entries =>
    2 : {name = kCFRunLoopDefaultMode, port set = 0x7403, queue = 0x608000176f80, source = 0x6080001d2840 (not fired), timer port = 0x7603, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = (null),
    currently 525108406 (10580015258087) / soft deadline in: 1.84467335e+10 sec (@ -1) / hard deadline in: 1.84467335e+10 sec (@ -1)
},

}
}

结论:输出的东西很多还很长,具体什么意思后面会和大家说,这里大家只要知道currentRunLoop1 = currentRunLoop2 = 。它们是两个不同的对象。


二、@property(readonly, copy) NSRunLoopMode currentMode;

这里currentMode的意思就是运行循环的模式,我们每一个RunLoop在运行的时候都是在某一个模式或者模式的组合上运行的。

这里NSRunLoopMode模式是一个字符串,runloop的模式,使得runloop显得更加灵活,适应更多的应用场景。如果定时器处于mode1,而runloop运行在mode2,则定时器不会触发,只有runloop运行在mode1时,定时器才会触发。

系统为我们提供了多种模式,下面列一些比较常遇到的:

  • kCFRunLoopDefaultMode: App的默认 Mode,通常主线程是在这个 Mode 下运行的。
  • UITrackingRunLoopMode: 界面跟踪 Mode,用于 ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响。
  • UIInitializationRunLoopMode: 在刚启动 App 时第进入的第一个 Mode,启动完成后就不再使用。
  • NSRunLoopCommonModes: 包含了多种模式:default, modal, 和tracking modes

这里只是简单的说了下这几个模式,后续会和大家详述。

下面看一下代码

- (void)demoCurrentMode
{
    NSLog(@"currentRunLoop1 = %@", [NSRunLoop currentRunLoop].currentMode);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"currentRunLoop2 = %@", [NSRunLoop currentRunLoop].currentMode);
    });
}

下面看输出结果

2017-08-23 00:11:54.867 JJOC[4198:127223] currentRunLoop1 = UIInitializationRunLoopMode
2017-08-23 00:11:54.867 JJOC[4198:127355] currentRunLoop2 = (null)

这里还需要注意:

  • 当前输入模式,该方法仅在接收器运行时返回当前输入模式; 否则返回零。
  • 当前模式由运行循环的方法设置,如acceptInputForMode:beforeDate:runMode:beforeDate :

结论:这个模式还是需要好好的理解的,很重要。


三、- (NSDate *)limitDateForMode:(NSRunLoopMode)mode;

该方法的作用是:在指定的模式下执行一次通过运行循环,并返回下一个定时器计划启动的日期。也可以这么理解为下一次的运行时间,如果在指定的mode上没有inputSource则输出nil。其实就是获取下一个响应时间。

定时器的执行,其实并不是按照时间段额间隔进行调用方法,而是在定时器注册到RunLoop中后,RunLoop会设置一个一个的时间点进行调用,例如,5,10,15,20等等。如果错过了某个时间点,定时器并不会延迟调用,而是直接等待下一个时间点调用,所以定时器并不是准确的。

下面我们看一下参数和返回值。

  • mode:运行循环模式进行搜索。 您可以指定自定义模式或使用运行循环模式Run Loop Modes中列出的其中一种模式。
  • return :下一个计时器的启动日期,如果没有此模式的输入源,则为零。

这里还需要注意:

  • 运行循环以立即超时输入,因此如果没有输入源需要处理,运行循环不会阻塞,等待输入。

结论:这个我也基本不会用到。


四、@property(class, readonly, strong) NSRunLoop *mainRunLoop;

这个属性的作用就是获取主线程的runloop对象,还是很好理解的,下面我们看代码。

- (void)demoMainRunLoop
{
    NSLog(@"mainRunLoop === %@", [NSRunLoop mainRunLoop]);
}

大家看一下输出结果

017-08-23 11:35:43.202149+0800 JJOC[10999:5290377] mainRunLoop === {wakeup port = 0x2103, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = {type = mutable set, count = 2,
entries =>
    0 : {contents = "UITrackingRunLoopMode"}
    2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = {type = mutable set, count = 16,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }
    2 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }
    3 : {valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}
    4 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    5 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x17013ea00, callout =  (0x18a6a9614)}}
    6 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}
    7 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2003, source = 0x17416c840, callout =  (0x186f98ee0), context = }}
    8 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17407b4c0, callout =  (0x18592ef34)}}
    9 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x170047860, callout =  (0x18a6aaa58)}}
    10 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }
    11 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
    12 : {signalled = No, valid = Yes, order = 0, context =  {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    15 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    19 : {valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }
    22 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x17416c9c0, callout =  (0x186f98fec), context = }}
}
,
modes = {type = mutable set, count = 5,
entries =>
    2 : {name = UITrackingRunLoopMode, port set = 0x2703, queue = 0x17416ca80, source = 0x1741dd4c0 (not fired), timer port = 0x2903, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x170047860, callout =  (0x18a6aaa58)}}
    3 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17407b4c0, callout =  (0x18592ef34)}}
    5 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x17013ea00, callout =  (0x18a6a9614)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : {signalled = No, valid = Yes, order = 0, context =  {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    2 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    4 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x17416c9c0, callout =  (0x186f98fec), context = }}
    5 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2003, source = 0x17416c840, callout =  (0x186f98ee0), context = }}
    6 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = (null),
    currently 525152143 (35300544762839) / soft deadline in: 7.68612866e+11 sec (@ -1) / hard deadline in: 7.68612866e+11 sec (@ -1)
},

    3 : {name = GSEventReceiveRunLoopMode, port set = 0x2e0b, queue = 0x17016cfc0, source = 0x1701dce30 (not fired), timer port = 0x3003, 
    sources0 = {type = mutable set, count = 1,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
}
,
    sources1 = {type = mutable set, count = 1,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
}
,
    observers = (null),
    timers = (null),
    currently 525152143 (35300544795114) / soft deadline in: 7.68612866e+11 sec (@ -1) / hard deadline in: 7.68612866e+11 sec (@ -1)
},

    4 : {name = kCFRunLoopDefaultMode, port set = 0x2203, queue = 0x17416c900, source = 0x1741dd1f0 (not fired), timer port = 0x2403, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x170047860, callout =  (0x18a6aaa58)}}
    3 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17407b4c0, callout =  (0x18592ef34)}}
    5 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x17013ea00, callout =  (0x18a6a9614)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : {signalled = No, valid = Yes, order = 0, context =  {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    2 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    4 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2503, source = 0x17416c9c0, callout =  (0x186f98fec), context = }}
    5 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2003, source = 0x17416c840, callout =  (0x186f98ee0), context = }}
    6 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = {type = mutable-small, count = 1, values = (
    0 : {valid = Yes, firing = No, interval = 0, tolerance = 0, next fire date = 525152145 (1.37681502 @ 35300577893733), callout = (Delayed Perform) UIApplication _accessibilitySetUpQuickSpeak (0x184863a64 / 0x189f3ac2c) (/System/Library/Frameworks/UIKit.framework/UIKit), context = }
)},
    currently 525152143 (35300544796303) / soft deadline in: 1.37905954 sec (@ 35300577893733) / hard deadline in: 1.3790595 sec (@ 35300577893733)
},

    5 : {name = UIInitializationRunLoopMode, port set = 0x3d03, queue = 0x17416d140, source = 0x1741dd790 (not fired), timer port = 0x3f03, 
    sources0 = {type = mutable set, count = 1,
entries =>
    1 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17407b4c0, callout =  (0x18592ef34)}}
}
,
    sources1 = {type = mutable set, count = 0,
entries =>
}
,
    observers = (
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }"
),
    timers = (null),
    currently 525152143 (35300544850972) / soft deadline in: 7.68612866e+11 sec (@ -1) / hard deadline in: 7.68612866e+11 sec (@ -1)
},

    6 : {name = kCFRunLoopCommonModes, port set = 0x560f, queue = 0x17416dec0, source = 0x1741dda60 (not fired), timer port = 0x5903, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = (null),
    currently 525152143 (35300544853174) / soft deadline in: 7.68612866e+11 sec (@ -1) / hard deadline in: 7.68612866e+11 sec (@ -1)
},

}
}

结论:简单易用,但是可以不用深究。


五、- (CFRunLoopRef)getCFRunLoop;

该方法的作用就是返回底层的CFRunLoop对象。

这里还要注意:你可以使用返回的runloop来通过使用Core Foundation函数调用配置当前的运行循环。例如,您可以使用此功能设置运行循环观察器。

下面我们看代码。

- (void)demoGetCFRunLoop
{
    CFRunLoopRef *loopRef1 = [[NSRunLoop currentRunLoop] getCFRunLoop];
    NSLog(@"loopRef1 = %@", loopRef1);
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        CFRunLoopRef *loopRef2 = [[NSRunLoop currentRunLoop] getCFRunLoop];
        NSLog(@"loopRef2 = %@", loopRef2);
    });
}

下面看输出结果

loopRef1

2017-08-23 11:48:32.070252+0800 JJOC[11006:5293944] loopRef1 = {wakeup port = 0x2203, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = {type = mutable set, count = 2,
entries =>
    0 : {contents = "UITrackingRunLoopMode"}
    2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = {type = mutable set, count = 16,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x17004c0c0, callout =  (0x18a6aaa58)}}
    2 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2603, source = 0x170166d80, callout =  (0x186f98fec), context = }}
    3 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17406c800, callout =  (0x18592ef34)}}
    4 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    5 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}
    6 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }
    7 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    10 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2103, source = 0x170166c00, callout =  (0x186f98ee0), context = }}
    15 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    17 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x170137a20, callout =  (0x18a6a9614)}}
    18 : {valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }
    19 : {signalled = No, valid = Yes, order = 0, context =  {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
    20 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }
    21 : {valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }
    22 : {valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}
}
,
modes = {type = mutable set, count = 5,
entries =>
    2 : {name = UITrackingRunLoopMode, port set = 0x2903, queue = 0x174166900, source = 0x1741d2840 (not fired), timer port = 0x2b03, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17406c800, callout =  (0x18592ef34)}}
    2 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x170137a20, callout =  (0x18a6a9614)}}
    6 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x17004c0c0, callout =  (0x18a6aaa58)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2603, source = 0x170166d80, callout =  (0x186f98fec), context = }}
    2 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2103, source = 0x170166c00, callout =  (0x186f98ee0), context = }}
    3 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    4 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    5 : {signalled = No, valid = Yes, order = 0, context =  {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = (null),
    currently 525152912 (35318997621563) / soft deadline in: 7.68612865e+11 sec (@ -1) / hard deadline in: 7.68612865e+11 sec (@ -1)
},

    3 : {name = GSEventReceiveRunLoopMode, port set = 0x2e0b, queue = 0x170167140, source = 0x1701d2c00 (not fired), timer port = 0x3003, 
    sources0 = {type = mutable set, count = 1,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
}
,
    sources1 = {type = mutable set, count = 1,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
}
,
    observers = (null),
    timers = (null),
    currently 525152912 (35318997655243) / soft deadline in: 7.68612865e+11 sec (@ -1) / hard deadline in: 7.68612865e+11 sec (@ -1)
},

    4 : {name = kCFRunLoopDefaultMode, port set = 0x2303, queue = 0x170166cc0, source = 0x1701d2930 (not fired), timer port = 0x2503, 
    sources0 = {type = mutable set, count = 4,
entries =>
    0 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x0, callout =  (0x1856cab5c)}}
    1 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17406c800, callout =  (0x18592ef34)}}
    2 : {signalled = No, valid = Yes, order = -1, context = {version = 0, info = 0x170137a20, callout =  (0x18a6a9614)}}
    6 : {signalled = Yes, valid = Yes, order = -2, context = {version = 0, info = 0x17004c0c0, callout =  (0x18a6aaa58)}}
}
,
    sources1 = {type = mutable set, count = 6,
entries =>
    0 : {signalled = No, valid = Yes, order = 0, context =  {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2603, source = 0x170166d80, callout =  (0x186f98fec), context = }}
    2 : {signalled = No, valid = Yes, order = 0, context = {valid = Yes, port = 2103, source = 0x170166c00, callout =  (0x186f98ee0), context = }}
    3 : {signalled = No, valid = Yes, order = -1, context = {version = 1, info = 0x3603, callout =  (0x1856cd370)}}
    4 : {signalled = No, valid = Yes, order = 0, context =  {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    5 : {signalled = No, valid = Yes, order = 0, context =  {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
}
,
    observers = (
    "{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}",
    "{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout =  (0x189ea8698), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout =  (0x18a156150), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout =  (0x189ea86a0), context = }",
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout =  (0x189ea8824), context = {type = mutable-small, count = 0, values = ()}}"
),
    timers = {type = mutable-small, count = 1, values = (
    0 : {valid = Yes, firing = No, interval = 0, tolerance = 0, next fire date = 525152913 (1.37859005 @ 35319030797578), callout = (Delayed Perform) UIApplication _accessibilitySetUpQuickSpeak (0x184863a64 / 0x189f3ac2c) (/System/Library/Frameworks/UIKit.framework/UIKit), context = }
)},
    currently 525152912 (35318997656551) / soft deadline in: 1.38087608 sec (@ 35319030797578) / hard deadline in: 1.38087604 sec (@ 35319030797578)
},

    5 : {name = UIInitializationRunLoopMode, port set = 0x3c03, queue = 0x174166d80, source = 0x1741d2b10 (not fired), timer port = 0x3e03, 
    sources0 = {type = mutable set, count = 1,
entries =>
    2 : {signalled = Yes, valid = Yes, order = 0, context = {version = 0, info = 0x17406c800, callout =  (0x18592ef34)}}
}
,
    sources1 = {type = mutable set, count = 0,
entries =>
}
,
    observers = (
    "{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout =  (0x18703ae14), context = }"
),
    timers = (null),
    currently 525152912 (35318997712510) / soft deadline in: 7.68612865e+11 sec (@ -1) / hard deadline in: 7.68612865e+11 sec (@ -1)
},

    6 : {name = kCFRunLoopCommonModes, port set = 0x5807, queue = 0x170168640, source = 0x1701d31a0 (not fired), timer port = 0x5903, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = (null),
    currently 525152912 (35318997714832) / soft deadline in: 7.68612865e+11 sec (@ -1) / hard deadline in: 7.68612865e+11 sec (@ -1)
},

}
}

loopRef2

2017-08-23 11:48:32.078486+0800 JJOC[11006:5293985] loopRef2 = {wakeup port = 0x4d0f, stopped = false, ignoreWakeUps = true, 
current mode = (none),
common modes = {type = mutable set, count = 1,
entries =>
    2 : {contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = {type = mutable set, count = 1,
entries =>
    2 : {name = kCFRunLoopDefaultMode, port set = 0x560f, queue = 0x174167500, source = 0x1741d3650 (not fired), timer port = 0x520b, 
    sources0 = (null),
    sources1 = (null),
    observers = (null),
    timers = (null),
    currently 525152912 (35318997914397) / soft deadline in: 7.68612865e+11 sec (@ -1) / hard deadline in: 7.68612865e+11 sec (@ -1)
},

}
}

结论:如果没有特殊的需求,一般也用不到这个底层,但是研究下还是好的。

参考文章

1. NSRunloop一点理解
2. iOS开发RunLoop学习:一:RunLoop简单介绍

后记

未完,待续~~~

NSRunloop简单细说(二)—— 获取运行循环及其模式_第1张图片

你可能感兴趣的:(NSRunloop简单细说(二)—— 获取运行循环及其模式)