RunLoop

来源:互联网 发布:内网访问阿里云 编辑:程序博客网 时间:2024/06/05 09:37

基本作用:
1.保持程序的持续运行(如果没有它程序在运行UIAplicationMain之后就结束了)
2.处理App中的各种事件(比如触摸、定时器、Selector)
3.节省CPU资源,提高程序的性能:有任务就做,没事就休息

RunLoop应用
1.NSTimer
2.PerformSelector
3.常驻线程
4.自动释放池

runloop的对象内容- (IBAction)click:(UIButton *)sender {    NSLog(@"%@",[NSRunLoop currentRunLoop]);}

打印结果为

2016-04-17 00:24:23.255 testRunLoop[62336:5558571] <CFRunLoop 0x7fb0e2501ca0 [0x1103fc7b0]>{wakeup port = 0x1403, stopped = false, ignoreWakeUps = false, current mode = kCFRunLoopDefaultMode,common modes = <CFBasicHash 0x7fb0e2501ba0 [0x1103fc7b0]>{type = mutable set, count = 2,entries =>0 : <CFString 0x1112f7270 [0x1103fc7b0]>{contents = "UITrackingRunLoopMode"}2 : <CFString 0x11041cb60 [0x1103fc7b0]>{contents = "kCFRunLoopDefaultMode"}},common mode items = <CFBasicHash 0x7fb0e2501f30 [0x1103fc7b0]>{type = mutable set, count = 17,entries =>0 : <CFRunLoopSource 0x7fb0e2707080 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x113993779)}}1 : <CFRunLoopSource 0x7fb0e2602af0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e27068d0 [0x1103fc7b0]>{valid = Yes, port = 1e03, source = 0x7fb0e2602af0, callout = __IOHIDEventSystemClientAvailabilityCallback (0x11261aa2f), context = <CFMachPort context 0x7fb0e2403930>}}2 : <CFRunLoopObserver 0x7fb0e26065c0 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x1105afa99), context = <CFRunLoopObserver context 0x7fb0e2707730>}3 : <CFRunLoopObserver 0x7fb0e260b1c0 [0x1103fc7b0]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}}4 : <CFRunLoopObserver 0x7fb0e2413d70 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x11411e320), context = <CFRunLoopObserver context 0x0>}7 : <CFRunLoopObserver 0x7fb0e25d5ee0 [0x1103fc7b0]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x110a5c6ab), context = <CFRunLoopObserver context 0x0>}8 : <CFRunLoopSource 0x7fb0e260a350 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e2707730, callout = _UIApplicationHandleEventQueue (0x11057d2db)}}9 : <CFRunLoopSource 0x7fb0e2501be0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2501880 [0x1103fc7b0]>{valid = Yes, port = 1007, source = 0x7fb0e2501be0, callout = _ZL20notify_port_callbackP12__CFMachPortPvlS1_ (0x11412e8a9), context = <CFMachPort context 0x0>}}10 : <CFRunLoopSource 0x7fb0e250e510 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 20227, subsystem = 0x1135f3f70, context = 0x7fb0e2504330}}11 : <CFRunLoopSource 0x7fb0e2601ab0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 14087, subsystem = 0x1112c9fe0, context = 0x0}}13 : <CFRunLoopObserver 0x7fb0e260ab40 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x1105afa54), context = <CFRunLoopObserver context 0x7fb0e2707730>}14 : <CFRunLoopSource 0x7fb0e2503c30 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x2203, callout = PurpleEventCallback (0x113995cb0)}}15 : <CFRunLoopSource 0x7fb0e2706d60 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 1, context = <CFMachPort 0x7fb0e26028a0 [0x1103fc7b0]>{valid = Yes, port = 1b03, source = 0x7fb0e2706d60, callout = __IOMIGMachPortPortCallback (0x112622fce), context = <CFMachPort context 0x7fb0e2602730>}}16 : <CFRunLoopObserver 0x7fb0e2608260 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}}20 : <CFRunLoopSource 0x7fb0e25d9a70 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 18443, subsystem = 0x1112dc920, context = 0x7fb0e27055d0}}21 : <CFRunLoopSource 0x7fb0e240ae00 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e240abb0, callout = FBSSerialQueueRunLoopSourceHandler (0x112f52f60)}}22 : <CFRunLoopSource 0x7fb0e2706c20 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2403ba0 [0x1103fc7b0]>{valid = Yes, port = 1d03, source = 0x7fb0e2706c20, callout = __IOHIDEventSystemClientQueueCallback (0x11261a87e), context = <CFMachPort context 0x7fb0e2403930>}}},modes = <CFBasicHash 0x7fb0e2501d80 [0x1103fc7b0]>{type = mutable set, count = 5,entries =>2 : <CFRunLoopMode 0x7fb0e2704d20 [0x1103fc7b0]>{name = UITrackingRunLoopMode, port set = 0x1707, timer port = 0x1803, sources0 = <CFBasicHash 0x7fb0e2705500 [0x1103fc7b0]>{type = mutable set, count = 3,entries =>0 : <CFRunLoopSource 0x7fb0e2707080 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x113993779)}}1 : <CFRunLoopSource 0x7fb0e240ae00 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e240abb0, callout = FBSSerialQueueRunLoopSourceHandler (0x112f52f60)}}2 : <CFRunLoopSource 0x7fb0e260a350 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e2707730, callout = _UIApplicationHandleEventQueue (0x11057d2db)}}},sources1 = <CFBasicHash 0x7fb0e2704fc0 [0x1103fc7b0]>{type = mutable set, count = 8,entries =>0 : <CFRunLoopSource 0x7fb0e2602af0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e27068d0 [0x1103fc7b0]>{valid = Yes, port = 1e03, source = 0x7fb0e2602af0, callout = __IOHIDEventSystemClientAvailabilityCallback (0x11261aa2f), context = <CFMachPort context 0x7fb0e2403930>}}1 : <CFRunLoopSource 0x7fb0e250e510 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 20227, subsystem = 0x1135f3f70, context = 0x7fb0e2504330}}4 : <CFRunLoopSource 0x7fb0e2706c20 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2403ba0 [0x1103fc7b0]>{valid = Yes, port = 1d03, source = 0x7fb0e2706c20, callout = __IOHIDEventSystemClientQueueCallback (0x11261a87e), context = <CFMachPort context 0x7fb0e2403930>}}8 : <CFRunLoopSource 0x7fb0e2501be0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2501880 [0x1103fc7b0]>{valid = Yes, port = 1007, source = 0x7fb0e2501be0, callout = _ZL20notify_port_callbackP12__CFMachPortPvlS1_ (0x11412e8a9), context = <CFMachPort context 0x0>}}9 : <CFRunLoopSource 0x7fb0e2601ab0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 14087, subsystem = 0x1112c9fe0, context = 0x0}}10 : <CFRunLoopSource 0x7fb0e2503c30 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x2203, callout = PurpleEventCallback (0x113995cb0)}}11 : <CFRunLoopSource 0x7fb0e25d9a70 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 18443, subsystem = 0x1112dc920, context = 0x7fb0e27055d0}}12 : <CFRunLoopSource 0x7fb0e2706d60 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 1, context = <CFMachPort 0x7fb0e26028a0 [0x1103fc7b0]>{valid = Yes, port = 1b03, source = 0x7fb0e2706d60, callout = __IOMIGMachPortPortCallback (0x112622fce), context = <CFMachPort context 0x7fb0e2602730>}}},observers = <CFArray 0x7fb0e260abe0 [0x1103fc7b0]>{type = mutable-small, count = 6, values = (0 : <CFRunLoopObserver 0x7fb0e260b1c0 [0x1103fc7b0]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}}1 : <CFRunLoopObserver 0x7fb0e25d5ee0 [0x1103fc7b0]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x110a5c6ab), context = <CFRunLoopObserver context 0x0>}2 : <CFRunLoopObserver 0x7fb0e260ab40 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x1105afa54), context = <CFRunLoopObserver context 0x7fb0e2707730>}3 : <CFRunLoopObserver 0x7fb0e2413d70 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x11411e320), context = <CFRunLoopObserver context 0x0>}4 : <CFRunLoopObserver 0x7fb0e26065c0 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x1105afa99), context = <CFRunLoopObserver context 0x7fb0e2707730>}5 : <CFRunLoopObserver 0x7fb0e2608260 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}})},timers = (null),currently 482516663 (60061606162459) / soft deadline in: 1.8446684e+10 sec (@ -1) / hard deadline in: 1.8446684e+10 sec (@ -1)},3 : <CFRunLoopMode 0x7fb0e2602ca0 [0x1103fc7b0]>{name = GSEventReceiveRunLoopMode, port set = 0x2003, timer port = 0x2103, sources0 = <CFBasicHash 0x7fb0e2602bb0 [0x1103fc7b0]>{type = mutable set, count = 1,entries =>0 : <CFRunLoopSource 0x7fb0e2707080 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x113993779)}}},sources1 = <CFBasicHash 0x7fb0e2602d50 [0x1103fc7b0]>{type = mutable set, count = 1,entries =>1 : <CFRunLoopSource 0x7fb0e2707140 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x2203, callout = PurpleEventCallback (0x113995cb0)}}},observers = (null),timers = (null),currently 482516663 (60061607909020) / soft deadline in: 1.8446684e+10 sec (@ -1) / hard deadline in: 1.8446684e+10 sec (@ -1)},4 : <CFRunLoopMode 0x7fb0e2501dc0 [0x1103fc7b0]>{name = kCFRunLoopDefaultMode, port set = 0x1503, timer port = 0x1603, sources0 = <CFBasicHash 0x7fb0e2501f90 [0x1103fc7b0]>{type = mutable set, count = 3,entries =>0 : <CFRunLoopSource 0x7fb0e2707080 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x0, callout = PurpleEventSignalCallback (0x113993779)}}1 : <CFRunLoopSource 0x7fb0e240ae00 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e240abb0, callout = FBSSerialQueueRunLoopSourceHandler (0x112f52f60)}}2 : <CFRunLoopSource 0x7fb0e260a350 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e2707730, callout = _UIApplicationHandleEventQueue (0x11057d2db)}}},sources1 = <CFBasicHash 0x7fb0e2501fd0 [0x1103fc7b0]>{type = mutable set, count = 8,entries =>0 : <CFRunLoopSource 0x7fb0e2602af0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e27068d0 [0x1103fc7b0]>{valid = Yes, port = 1e03, source = 0x7fb0e2602af0, callout = __IOHIDEventSystemClientAvailabilityCallback (0x11261aa2f), context = <CFMachPort context 0x7fb0e2403930>}}1 : <CFRunLoopSource 0x7fb0e250e510 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 20227, subsystem = 0x1135f3f70, context = 0x7fb0e2504330}}4 : <CFRunLoopSource 0x7fb0e2706c20 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2403ba0 [0x1103fc7b0]>{valid = Yes, port = 1d03, source = 0x7fb0e2706c20, callout = __IOHIDEventSystemClientQueueCallback (0x11261a87e), context = <CFMachPort context 0x7fb0e2403930>}}8 : <CFRunLoopSource 0x7fb0e2501be0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFMachPort 0x7fb0e2501880 [0x1103fc7b0]>{valid = Yes, port = 1007, source = 0x7fb0e2501be0, callout = _ZL20notify_port_callbackP12__CFMachPortPvlS1_ (0x11412e8a9), context = <CFMachPort context 0x0>}}9 : <CFRunLoopSource 0x7fb0e2601ab0 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 14087, subsystem = 0x1112c9fe0, context = 0x0}}10 : <CFRunLoopSource 0x7fb0e2503c30 [0x1103fc7b0]>{signalled = No, valid = Yes, order = -1, context = <CFRunLoopSource context>{version = 1, info = 0x2203, callout = PurpleEventCallback (0x113995cb0)}}11 : <CFRunLoopSource 0x7fb0e25d9a70 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource MIG Server> {port = 18443, subsystem = 0x1112dc920, context = 0x7fb0e27055d0}}12 : <CFRunLoopSource 0x7fb0e2706d60 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 1, context = <CFMachPort 0x7fb0e26028a0 [0x1103fc7b0]>{valid = Yes, port = 1b03, source = 0x7fb0e2706d60, callout = __IOMIGMachPortPortCallback (0x112622fce), context = <CFMachPort context 0x7fb0e2602730>}}},observers = <CFArray 0x7fb0e2606500 [0x1103fc7b0]>{type = mutable-small, count = 6, values = (0 : <CFRunLoopObserver 0x7fb0e260b1c0 [0x1103fc7b0]>{valid = Yes, activities = 0x1, repeats = Yes, order = -2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}}1 : <CFRunLoopObserver 0x7fb0e25d5ee0 [0x1103fc7b0]>{valid = Yes, activities = 0x20, repeats = Yes, order = 0, callout = _UIGestureRecognizerUpdateObserver (0x110a5c6ab), context = <CFRunLoopObserver context 0x0>}2 : <CFRunLoopObserver 0x7fb0e260ab40 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 1999000, callout = _beforeCACommitHandler (0x1105afa54), context = <CFRunLoopObserver context 0x7fb0e2707730>}3 : <CFRunLoopObserver 0x7fb0e2413d70 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x11411e320), context = <CFRunLoopObserver context 0x0>}4 : <CFRunLoopObserver 0x7fb0e26065c0 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2001000, callout = _afterCACommitHandler (0x1105afa99), context = <CFRunLoopObserver context 0x7fb0e2707730>}5 : <CFRunLoopObserver 0x7fb0e2608260 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2147483647, callout = _wrapRunLoopWithAutoreleasePoolHandler (0x11057cc4e), context = <CFArray 0x7fb0e26064d0 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <0x7fb0e2809048>)}})},timers = <CFArray 0x7fb0e240c340 [0x1103fc7b0]>{type = mutable-small, count = 0, values = ()},currently 482516663 (60061608024136) / soft deadline in: 1.8446684e+10 sec (@ -1) / hard deadline in: 1.8446684e+10 sec (@ -1)},5 : <CFRunLoopMode 0x7fb0e240aff0 [0x1103fc7b0]>{name = UIInitializationRunLoopMode, port set = 0x2e0b, timer port = 0x310b, sources0 = <CFBasicHash 0x7fb0e240b0a0 [0x1103fc7b0]>{type = mutable set, count = 1,entries =>1 : <CFRunLoopSource 0x7fb0e240ae00 [0x1103fc7b0]>{signalled = No, valid = Yes, order = 0, context = <CFRunLoopSource context>{version = 0, info = 0x7fb0e240abb0, callout = FBSSerialQueueRunLoopSourceHandler (0x112f52f60)}}},sources1 = <CFBasicHash 0x7fb0e240b0e0 [0x1103fc7b0]>{type = mutable set, count = 0,entries =>},observers = <CFArray 0x7fb0e2413e70 [0x1103fc7b0]>{type = mutable-small, count = 1, values = (0 : <CFRunLoopObserver 0x7fb0e2413d70 [0x1103fc7b0]>{valid = Yes, activities = 0xa0, repeats = Yes, order = 2000000, callout = _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv (0x11411e320), context = <CFRunLoopObserver context 0x0>})},timers = (null),currently 482516663 (60061610182696) / soft deadline in: 1.8446684e+10 sec (@ -1) / hard deadline in: 1.8446684e+10 sec (@ -1)},6 : <CFRunLoopMode 0x7fb0e2512fc0 [0x1103fc7b0]>{name = kCFRunLoopCommonModes, port set = 0x3f03, timer port = 0x4003, sources0 = (null),sources1 = (null),observers = (null),timers = (null),currently 482516663 (60061610309424) / soft deadline in: 1.8446684e+10 sec (@ -1) / hard deadline in: 1.8446684e+10 sec (@ -1)},}}
0 0
原创粉丝点击