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 = <CFRunLoop 0x6000001756c0 [0x1058bbe40]>{wakeup port = 0x2103, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = <CFBasicHash 0x60000005d850 [0x1058bbe40]>{type = mutable set, count = 2,
entries =>
    0 : <CFString 0x107e4c6c0 [0x1058bbe40]>{contents = "UITrackingRunLoopMode"}
    2 : <CFString 0x105893970 [0x1058bbe40]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x60000005d700 [0x1058bbe40]>{type = mutable set, count = 16,
entries =>
    0 : <CFRunLoopSource 0x608000175480 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    1 : <CFRunLoopObserver 0x608000136800 [0x1058bbe40]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{type = mutable-small, count = 0, values = ()}}
    2 : <CFRunLoopObserver 0x6080001368a0 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{type = mutable-small, count = 0, values = ()}}
    3 : <CFRunLoopSource 0x608000176080 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : <CFRunLoopSource 0x608000176e00 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    5 : <CFRunLoopSource 0x600000176500 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    7 : <CFRunLoopSource 0x600000175cc0 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
    9 : <CFRunLoopObserver 0x608000136300 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = <CFRunLoopObserver context 0x0>}
    10 : <CFRunLoopObserver 0x608000136260 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = <CFRunLoopObserver context 0x7fa063f00ac0>}
    11 : <CFRunLoopSource 0x608000175c00 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
    12 : <CFRunLoopSource 0x608000176140 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    13 : <CFRunLoopObserver 0x608000136080 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = <CFRunLoopObserver context 0x7fa063f00ac0>}
    14 : <CFRunLoopSource 0x608000176a40 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    18 : <CFRunLoopSource 0x600000175780 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b6a0 [0x1058bbe40]>{valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = <CFMachPort context 0x0>}}
    21 : <CFRunLoopObserver 0x600000136d00 [0x1058bbe40]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = <CFRunLoopObserver context 0x6000000da470>}
    22 : <CFRunLoopSource 0x600000175900 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b5f0 [0x1058bbe40]>{valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = <CFMachPort context 0x0>}}
}
,
modes = <CFBasicHash 0x60000005d820 [0x1058bbe40]>{type = mutable set, count = 5,
entries =>
    2 : <CFRunLoopMode 0x6000001933f0 [0x1058bbe40]>{name = UITrackingRunLoopMode, port set = 0x2703, queue = 0x600000175c00, source = 0x6000001d1580 (not fired), timer port = 0x2903, 
    sources0 = <CFBasicHash 0x60000005d5b0 [0x1058bbe40]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x608000175480 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    3 : <CFRunLoopSource 0x608000176080 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : <CFRunLoopSource 0x600000176500 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    5 : <CFRunLoopSource 0x608000175c00 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
}
,
    sources1 = <CFBasicHash 0x60000005d580 [0x1058bbe40]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x608000176e00 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    1 : <CFRunLoopSource 0x608000176140 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    3 : <CFRunLoopSource 0x600000175780 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b6a0 [0x1058bbe40]>{valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = <CFMachPort context 0x0>}}
    4 : <CFRunLoopSource 0x600000175900 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b5f0 [0x1058bbe40]>{valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = <CFMachPort context 0x0>}}
    5 : <CFRunLoopSource 0x608000176a40 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    6 : <CFRunLoopSource 0x600000175cc0 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
}
,
    observers = (
    "<CFRunLoopObserver 0x608000136800 [0x1058bbe40]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x600000136d00 [0x1058bbe40]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = <CFRunLoopObserver context 0x6000000da470>}",
    "<CFRunLoopObserver 0x608000136080 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = <CFRunLoopObserver context 0x7fa063f00ac0>}",
    "<CFRunLoopObserver 0x608000136300 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x608000136260 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = <CFRunLoopObserver context 0x7fa063f00ac0>}",
    "<CFRunLoopObserver 0x6080001368a0 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{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 : <CFRunLoopMode 0x608000193590 [0x1058bbe40]>{name = GSEventReceiveRunLoopMode, port set = 0x2f03, queue = 0x608000175540, source = 0x6080001d2570 (not fired), timer port = 0x3103, 
    sources0 = <CFBasicHash 0x60800005e9c0 [0x1058bbe40]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopSource 0x608000175480 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
}
,
    sources1 = <CFBasicHash 0x60800005e990 [0x1058bbe40]>{type = mutable set, count = 1,
entries =>
    1 : <CFRunLoopSource 0x600000175d80 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource 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 : <CFRunLoopMode 0x6000001934c0 [0x1058bbe40]>{name = kCFRunLoopDefaultMode, port set = 0x2203, queue = 0x600000175840, source = 0x6000001d13a0 (not fired), timer port = 0x2403, 
    sources0 = <CFBasicHash 0x60000005d6d0 [0x1058bbe40]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x608000175480 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x10ae9a723)}}
    3 : <CFRunLoopSource 0x608000176080 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x600000136bc0, callout = __handleEventQueue (0x1077840d0)}}
    4 : <CFRunLoopSource 0x600000176500 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x600000262700, callout = FBSSerialQueueRunLoopSourceHandler (0x10c526ca6)}}
    5 : <CFRunLoopSource 0x608000175c00 [0x1058bbe40]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x60000005eae0, callout = __handleHIDEventFetcherDrain (0x10778588c)}}
}
,
    sources1 = <CFBasicHash 0x60000005d6a0 [0x1058bbe40]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x608000176e00 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 29187, subsystem = 0x10c90bf60, context = 0x6000000ba460}}
    1 : <CFRunLoopSource 0x608000176140 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 23311, subsystem = 0x107e12b10, context = 0x0}}
    3 : <CFRunLoopSource 0x600000175780 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b6a0 [0x1058bbe40]>{valid = Yes, port = 1c03, source = 0x600000175780, callout = _ZL27change_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627f9f7), context = <CFMachPort context 0x0>}}
    4 : <CFRunLoopSource 0x600000175900 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x60000015b5f0 [0x1058bbe40]>{valid = Yes, port = 2503, source = 0x600000175900, callout = _ZL26power_notify_port_callbackP12__CFMachPortPvlS1_ (0x10627fb06), context = <CFMachPort context 0x0>}}
    5 : <CFRunLoopSource 0x608000176a40 [0x1058bbe40]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 27403, subsystem = 0x107e294f0, context = 0x60800003e280}}
    6 : <CFRunLoopSource 0x600000175cc0 [0x1058bbe40]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3703, callout = PurpleEventCallback (0x10ae9cc58)}}
}
,
    observers = (
    "<CFRunLoopObserver 0x608000136800 [0x1058bbe40]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x600000136d00 [0x1058bbe40]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x1074adb1b), context = <CFRunLoopObserver context 0x6000000da470>}",
    "<CFRunLoopObserver 0x608000136080 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x106fb645c), context = <CFRunLoopObserver context 0x7fa063f00ac0>}",
    "<CFRunLoopObserver 0x608000136300 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x106320ac4), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x608000136260 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x106fb64d7), context = <CFRunLoopObserver context 0x7fa063f00ac0>}",
    "<CFRunLoopObserver 0x6080001368a0 [0x1058bbe40]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x106f83de6), context = <CFArray 0x60800005f410 [0x1058bbe40]>{type = mutable-small, count = 0, values = ()}}"
),
    timers = <CFArray 0x6000000ba6a0 [0x1058bbe40]>{type = mutable-small, count = 1, values = (
    0 : <CFRunLoopTimer 0x600000175fc0 [0x1058bbe40]>{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 = <CFRunLoopTimer context 0x600000260500>}
)},
    currently 525108406 (10580009118217) / soft deadline in: 1.41481117 sec (@ 10581423929411) / hard deadline in: 1.41481113 sec (@ 10581423929411)
},

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

    6 : <CFRunLoopMode 0x6000001938d0 [0x1058bbe40]>{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 = <CFRunLoop 0x608000176440 [0x1058bbe40]>{wakeup port = 0x7303, stopped = false, ignoreWakeUps = true, 
current mode = (none),
common modes = <CFBasicHash 0x60800005fb00 [0x1058bbe40]>{type = mutable set, count = 1,
entries =>
    2 : <CFString 0x105893970 [0x1058bbe40]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = <CFBasicHash 0x60800005fda0 [0x1058bbe40]>{type = mutable set, count = 1,
entries =>
    2 : <CFRunLoopMode 0x6080001946a0 [0x1058bbe40]>{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 = <CFRunLoop 0x6000001756c0...currentRunLoop2 = <CFRunLoop 0x608000176440...。它们是两个不同的对象。


二、@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 === <CFRunLoop 0x17416c780 [0x1aa03fbb8]>{wakeup port = 0x2103, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = <CFBasicHash 0x1740460f0 [0x1aa03fbb8]>{type = mutable set, count = 2,
entries =>
    0 : <CFString 0x1a48f4970 [0x1aa03fbb8]>{contents = "UITrackingRunLoopMode"}
    2 : <CFString 0x1a3e3c4b0 [0x1aa03fbb8]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x174046270 [0x1aa03fbb8]>{type = mutable set, count = 16,
entries =>
    0 : <CFRunLoopSource 0x17016cf00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopObserver 0x17013e8c0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x139d00dc0>}
    2 : <CFRunLoopObserver 0x17013e820 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x139d00dc0>}
    3 : <CFRunLoopObserver 0x17013e780 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}
    4 : <CFRunLoopSource 0x17416ccc0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    5 : <CFRunLoopSource 0x17016d800 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x17013ea00, callout = <redacted> (0x18a6a9614)}}
    6 : <CFRunLoopObserver 0x17013e5a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}
    7 : <CFRunLoopSource 0x17416c840 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x174146300 [0x1aa03fbb8]>{valid = Yes, port = 2003, source = 0x17416c840, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    8 : <CFRunLoopSource 0x17416d080 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17407b4c0, callout = <redacted> (0x18592ef34)}}
    9 : <CFRunLoopSource 0x17016d680 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x170047860, callout = <redacted> (0x18a6aaa58)}}
    10 : <CFRunLoopObserver 0x17013e500 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}
    11 : <CFRunLoopSource 0x17416e400 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
    12 : <CFRunLoopSource 0x17016e7c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    15 : <CFRunLoopSource 0x17016d8c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    19 : <CFRunLoopObserver 0x17013ebe0 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700d9280>}
    22 : <CFRunLoopSource 0x17416c9c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x1741463b0 [0x1aa03fbb8]>{valid = Yes, port = 2503, source = 0x17416c9c0, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
}
,
modes = <CFBasicHash 0x1740461e0 [0x1aa03fbb8]>{type = mutable set, count = 5,
entries =>
    2 : <CFRunLoopMode 0x174183dc0 [0x1aa03fbb8]>{name = UITrackingRunLoopMode, port set = 0x2703, queue = 0x17416ca80, source = 0x1741dd4c0 (not fired), timer port = 0x2903, 
    sources0 = <CFBasicHash 0x174046b10 [0x1aa03fbb8]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x17016cf00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopSource 0x17016d680 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x170047860, callout = <redacted> (0x18a6aaa58)}}
    3 : <CFRunLoopSource 0x17416d080 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17407b4c0, callout = <redacted> (0x18592ef34)}}
    5 : <CFRunLoopSource 0x17016d800 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x17013ea00, callout = <redacted> (0x18a6a9614)}}
}
,
    sources1 = <CFBasicHash 0x174046ae0 [0x1aa03fbb8]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x17016d8c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : <CFRunLoopSource 0x17016e7c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    2 : <CFRunLoopSource 0x17416ccc0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    4 : <CFRunLoopSource 0x17416c9c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x1741463b0 [0x1aa03fbb8]>{valid = Yes, port = 2503, source = 0x17416c9c0, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
    5 : <CFRunLoopSource 0x17416c840 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x174146300 [0x1aa03fbb8]>{valid = Yes, port = 2003, source = 0x17416c840, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    6 : <CFRunLoopSource 0x17416e400 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
}
,
    observers = (
    "<CFRunLoopObserver 0x17013e780 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x17013ebe0 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700d9280>}",
    "<CFRunLoopObserver 0x17013e8c0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x139d00dc0>}",
    "<CFRunLoopObserver 0x17013e500 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x17013e820 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x139d00dc0>}",
    "<CFRunLoopObserver 0x17013e5a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{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 : <CFRunLoopMode 0x170183c20 [0x1aa03fbb8]>{name = GSEventReceiveRunLoopMode, port set = 0x2e0b, queue = 0x17016cfc0, source = 0x1701dce30 (not fired), timer port = 0x3003, 
    sources0 = <CFBasicHash 0x1700476e0 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopSource 0x17016cf00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
}
,
    sources1 = <CFBasicHash 0x1700476b0 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopSource 0x17416cd80 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (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 : <CFRunLoopMode 0x174183cf0 [0x1aa03fbb8]>{name = kCFRunLoopDefaultMode, port set = 0x2203, queue = 0x17416c900, source = 0x1741dd1f0 (not fired), timer port = 0x2403, 
    sources0 = <CFBasicHash 0x174045d90 [0x1aa03fbb8]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x17016cf00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopSource 0x17016d680 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x170047860, callout = <redacted> (0x18a6aaa58)}}
    3 : <CFRunLoopSource 0x17416d080 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17407b4c0, callout = <redacted> (0x18592ef34)}}
    5 : <CFRunLoopSource 0x17016d800 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x17013ea00, callout = <redacted> (0x18a6a9614)}}
}
,
    sources1 = <CFBasicHash 0x1740460c0 [0x1aa03fbb8]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x17016d8c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : <CFRunLoopSource 0x17016e7c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26887, subsystem = 0x1a415ff90, context = 0x1700ac240}}
    2 : <CFRunLoopSource 0x17416ccc0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    4 : <CFRunLoopSource 0x17416c9c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x1741463b0 [0x1aa03fbb8]>{valid = Yes, port = 2503, source = 0x17416c9c0, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
    5 : <CFRunLoopSource 0x17416c840 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x174146300 [0x1aa03fbb8]>{valid = Yes, port = 2003, source = 0x17416c840, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    6 : <CFRunLoopSource 0x17416e400 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17403cee0}}
}
,
    observers = (
    "<CFRunLoopObserver 0x17013e780 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x17013ebe0 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700d9280>}",
    "<CFRunLoopObserver 0x17013e8c0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x139d00dc0>}",
    "<CFRunLoopObserver 0x17013e500 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x17013e820 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x139d00dc0>}",
    "<CFRunLoopObserver 0x17013e5a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x17005c1d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}"
),
    timers = <CFArray 0x1740abe20 [0x1aa03fbb8]>{type = mutable-small, count = 1, values = (
    0 : <CFRunLoopTimer 0x17416d800 [0x1aa03fbb8]>{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 = <CFRunLoopTimer context 0x17407ad80>}
)},
    currently 525152143 (35300544796303) / soft deadline in: 1.37905954 sec (@ 35300577893733) / hard deadline in: 1.3790595 sec (@ 35300577893733)
},

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

    6 : <CFRunLoopMode 0x174184920 [0x1aa03fbb8]>{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 = <CFRunLoop 0x170166b40 [0x1aa03fbb8]>{wakeup port = 0x2203, stopped = false, ignoreWakeUps = true, 
current mode = UIInitializationRunLoopMode,
common modes = <CFBasicHash 0x17004ae30 [0x1aa03fbb8]>{type = mutable set, count = 2,
entries =>
    0 : <CFString 0x1a48f4970 [0x1aa03fbb8]>{contents = "UITrackingRunLoopMode"}
    2 : <CFString 0x1a3e3c4b0 [0x1aa03fbb8]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = <CFBasicHash 0x17004ab00 [0x1aa03fbb8]>{type = mutable set, count = 16,
entries =>
    0 : <CFRunLoopSource 0x170167080 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopSource 0x1701684c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x17004c0c0, callout = <redacted> (0x18a6aaa58)}}
    2 : <CFRunLoopSource 0x170166d80 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158f70 [0x1aa03fbb8]>{valid = Yes, port = 2603, source = 0x170166d80, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
    3 : <CFRunLoopSource 0x1741669c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17406c800, callout = <redacted> (0x18592ef34)}}
    4 : <CFRunLoopSource 0x1701675c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    5 : <CFRunLoopObserver 0x170137520 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}
    6 : <CFRunLoopObserver 0x1701373e0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}
    7 : <CFRunLoopSource 0x1741678c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    10 : <CFRunLoopSource 0x170166c00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158ec0 [0x1aa03fbb8]>{valid = Yes, port = 2103, source = 0x170166c00, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    15 : <CFRunLoopSource 0x170168280 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    17 : <CFRunLoopSource 0x170168340 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x170137a20, callout = <redacted> (0x18a6a9614)}}
    18 : <CFRunLoopObserver 0x170137980 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700cdeb0>}
    19 : <CFRunLoopSource 0x174168100 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
    20 : <CFRunLoopObserver 0x170137840 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x100602130>}
    21 : <CFRunLoopObserver 0x1701377a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x100602130>}
    22 : <CFRunLoopObserver 0x170137700 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}
}
,
modes = <CFBasicHash 0x17004ad40 [0x1aa03fbb8]>{type = mutable set, count = 5,
entries =>
    2 : <CFRunLoopMode 0x174181110 [0x1aa03fbb8]>{name = UITrackingRunLoopMode, port set = 0x2903, queue = 0x174166900, source = 0x1741d2840 (not fired), timer port = 0x2b03, 
    sources0 = <CFBasicHash 0x17404b1c0 [0x1aa03fbb8]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x170167080 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopSource 0x1741669c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17406c800, callout = <redacted> (0x18592ef34)}}
    2 : <CFRunLoopSource 0x170168340 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x170137a20, callout = <redacted> (0x18a6a9614)}}
    6 : <CFRunLoopSource 0x1701684c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x17004c0c0, callout = <redacted> (0x18a6aaa58)}}
}
,
    sources1 = <CFBasicHash 0x17404b190 [0x1aa03fbb8]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x170168280 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : <CFRunLoopSource 0x170166d80 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158f70 [0x1aa03fbb8]>{valid = Yes, port = 2603, source = 0x170166d80, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
    2 : <CFRunLoopSource 0x170166c00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158ec0 [0x1aa03fbb8]>{valid = Yes, port = 2103, source = 0x170166c00, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    3 : <CFRunLoopSource 0x1701675c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    4 : <CFRunLoopSource 0x1741678c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    5 : <CFRunLoopSource 0x174168100 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
}
,
    observers = (
    "<CFRunLoopObserver 0x170137700 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x170137980 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700cdeb0>}",
    "<CFRunLoopObserver 0x170137840 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x100602130>}",
    "<CFRunLoopObserver 0x1701373e0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x1701377a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x100602130>}",
    "<CFRunLoopObserver 0x170137520 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{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 : <CFRunLoopMode 0x170181040 [0x1aa03fbb8]>{name = GSEventReceiveRunLoopMode, port set = 0x2e0b, queue = 0x170167140, source = 0x1701d2c00 (not fired), timer port = 0x3003, 
    sources0 = <CFBasicHash 0x17004bfa0 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopSource 0x170167080 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
}
,
    sources1 = <CFBasicHash 0x17004beb0 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    0 : <CFRunLoopSource 0x170167500 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (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 : <CFRunLoopMode 0x170180f70 [0x1aa03fbb8]>{name = kCFRunLoopDefaultMode, port set = 0x2303, queue = 0x170166cc0, source = 0x1701d2930 (not fired), timer port = 0x2503, 
    sources0 = <CFBasicHash 0x17004b190 [0x1aa03fbb8]>{type = mutable set, count = 4,
entries =>
    0 : <CFRunLoopSource 0x170167080 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = <redacted> (0x1856cab5c)}}
    1 : <CFRunLoopSource 0x1741669c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x17406c800, callout = <redacted> (0x18592ef34)}}
    2 : <CFRunLoopSource 0x170168340 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x170137a20, callout = <redacted> (0x18a6a9614)}}
    6 : <CFRunLoopSource 0x1701684c0 [0x1aa03fbb8]>{signalled = Yes, valid = Yes, order = -2, context = <CFRunLoopSource context>{version = 0, info = 0x17004c0c0, callout = <redacted> (0x18a6aaa58)}}
}
,
    sources1 = <CFBasicHash 0x17004ace0 [0x1aa03fbb8]>{type = mutable set, count = 6,
entries =>
    0 : <CFRunLoopSource 0x170168280 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 17955, subsystem = 0x1a48baba0, context = 0x0}}
    1 : <CFRunLoopSource 0x170166d80 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158f70 [0x1aa03fbb8]>{valid = Yes, port = 2603, source = 0x170166d80, callout = <redacted> (0x186f98fec), context = <CFMachPort context 0x0>}}
    2 : <CFRunLoopSource 0x170166c00 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x170158ec0 [0x1aa03fbb8]>{valid = Yes, port = 2103, source = 0x170166c00, callout = <redacted> (0x186f98ee0), context = <CFMachPort context 0x0>}}
    3 : <CFRunLoopSource 0x1701675c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x3603, callout = <redacted> (0x1856cd370)}}
    4 : <CFRunLoopSource 0x1741678c0 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 25351, subsystem = 0x1a48d1600, context = 0x17002ad80}}
    5 : <CFRunLoopSource 0x174168100 [0x1aa03fbb8]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 26883, subsystem = 0x1a415ff90, context = 0x1700b4a00}}
}
,
    observers = (
    "<CFRunLoopObserver 0x170137700 [0x1aa03fbb8]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}",
    "<CFRunLoopObserver 0x170137980 [0x1aa03fbb8]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = <redacted> (0x189ea8698), context = <CFRunLoopObserver context 0x1700cdeb0>}",
    "<CFRunLoopObserver 0x170137840 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = <redacted> (0x18a156150), context = <CFRunLoopObserver context 0x100602130>}",
    "<CFRunLoopObserver 0x1701373e0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = <redacted> (0x18703ae14), context = <CFRunLoopObserver context 0x0>}",
    "<CFRunLoopObserver 0x1701377a0 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = <redacted> (0x189ea86a0), context = <CFRunLoopObserver context 0x100602130>}",
    "<CFRunLoopObserver 0x170137520 [0x1aa03fbb8]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = <redacted> (0x189ea8824), context = <CFArray 0x1702405d0 [0x1aa03fbb8]>{type = mutable-small, count = 0, values = ()}}"
),
    timers = <CFArray 0x1740b4760 [0x1aa03fbb8]>{type = mutable-small, count = 1, values = (
    0 : <CFRunLoopTimer 0x174167440 [0x1aa03fbb8]>{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 = <CFRunLoopTimer context 0x17406cbc0>}
)},
    currently 525152912 (35318997656551) / soft deadline in: 1.38087608 sec (@ 35319030797578) / hard deadline in: 1.38087604 sec (@ 35319030797578)
},

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

    6 : <CFRunLoopMode 0x1701827d0 [0x1aa03fbb8]>{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 = <CFRunLoop 0x174167200 [0x1aa03fbb8]>{wakeup port = 0x4d0f, stopped = false, ignoreWakeUps = true, 
current mode = (none),
common modes = <CFBasicHash 0x17404bee0 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    2 : <CFString 0x1a3e3c4b0 [0x1aa03fbb8]>{contents = "kCFRunLoopDefaultMode"}
}
,
common mode items = (null),
modes = <CFBasicHash 0x17404bc70 [0x1aa03fbb8]>{type = mutable set, count = 1,
entries =>
    2 : <CFRunLoopMode 0x1741815f0 [0x1aa03fbb8]>{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简单介绍

后记

未完,待续~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容