{"id":1413,"date":"2018-11-24T12:24:03","date_gmt":"2018-11-24T04:24:03","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1413"},"modified":"2018-11-27T17:19:32","modified_gmt":"2018-11-27T09:19:32","slug":"%e5%88%a9%e7%94%a8runloop%e6%9d%a5%e5%ae%9e%e7%8e%b0%e7%ba%bf%e7%a8%8b%e7%9a%84%e4%bf%9d%e6%b4%bb%ef%bc%8c%e5%b9%b6%e5%b0%81%e8%a3%85%e5%ad%90%e7%ba%bf%e7%a8%8b%e4%bb%bb%e5%8a%a1%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2018\/11\/24\/1224031413.html","title":{"rendered":"\u5229\u7528RunLoop\u6765\u5b9e\u73b0\u7ebf\u7a0b\u7684\u4fdd\u6d3b\uff0c\u5e76\u5c01\u88c5\u5b50\u7ebf\u7a0b\u4efb\u52a1\u6267\u884c"},"content":{"rendered":"<p><strong>1\u3001\u57fa\u672c\u601d\u8def<\/strong><\/p>\n<p>1.1\u3001\u521b\u5efa\u4e00\u4e2aHelper\u7c7b\u5bf9NSThread\u7c7b\u8fdb\u884c\u5c01\u88c5\uff1b<br \/>\n1.2\u3001Helper\u7c7b\u5185\u90e8\u5b9e\u73b0\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u6267\u884c\u3001\u9500\u6bc1\u7b49\u4e00\u7cfb\u5217\u64cd\u4f5c\uff1b<br \/>\n1.3\u3001\u6838\u5fc3\u70b9\u5728\u4e8eRunLoop\u7684\u5faa\u73af\u6267\u884c\uff0c\u8c03\u7528runMode:beforeDate\u65b9\u6cd5\u8ba9\u7ebf\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u518d\u901a\u8fc7while\u5faa\u73af\uff0c\u4f7frunloop\u6267\u884c\u7ed3\u675f\u540e\u518d\u6b21\u5f00\u542f\uff0c\u5faa\u73af\u6761\u4ef6\u5916\u90e8\u63a7\u5236\uff0c\u8fd9\u6837\u5c31\u80fd\u8fbe\u5230\u63a7\u5236runloop\u7684\u751f\u547d\u5468\u671f\uff0c\u4ece\u800c\u4fdd\u5168\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff1b<br \/>\n1.4\u3001Helper\u7c7b\u5728\u9500\u6bc1\u65f6\uff0c\u8981\u53ca\u65f6\u66f4\u6539runloop\u7684\u5faa\u73af\u6761\u4ef6\uff0c\u5e76\u5524\u9192\u6216\u505c\u6b62\u5b50\u7ebf\u7a0b\u4e2d\u7684runloop\uff0crunloop\u505c\u6b62\u540e\u7ebf\u7a0b\u7ed3\u675f\uff0c\u5426\u5219\u4e24\u8005\u4f1a\u4e00\u76f4\u5b58\u5728\u5185\u5b58\u5f53\u4e2d\uff0crunloop\u56e0\u4e3a\u6ca1\u6709\u53ca\u65f6\u88ab\u5524\u9192\u800c\u4e00\u76f4\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u7ebf\u7a0b\u56e0\u4e3arunloop\u6ca1\u6709\u505c\u6b62\u800c\u65e0\u6cd5\u91ca\u653e\uff1b<br \/>\n1.5\u3001Helper\u5728\u4f7f\u7528NSThread\u521b\u5efa\u5b50\u7ebf\u7a0b\u65f6\u4e5f\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff1b<\/p>\n<p><strong>2\u3001\u4ee3\u7801\u793a\u4f8b<\/strong><!--more--><br \/>\n2.1\u3001\u5185\u90e8\u5b9e\u73b0<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\r\n\/\/  SAThreadHelper.h\r\n\/\/  RunLoop\r\n\/\/\r\n\/\/  Created by \u4f59\u897f\u5b89 on 2018\/11\/24.\r\n\/\/  Copyright \u00a9 2018 yusian. All rights reserved.\r\n\/\/\r\n\r\n#import \"SAThreadHelper.h\"\r\n@interface _SAInnerThread : NSThread\r\n@end\r\n@implementation _SAInnerThread\r\n- (instancetype)init\r\n{\r\n    if (self = [super init]){\r\n        self.name = @\"com.yusian.inner-thread\";\r\n    }\r\n    return self;\r\n}\r\n- (void)dealloc\r\n{\r\n    NSLog(@\"%s\", __func__);\r\n}\r\n@end\r\n\r\n@interface SAThreadHelper ()\r\n@property (nonatomic, assign, getter=isStopped) BOOL    stopped;\r\n@property (nonatomic, strong)   _SAInnerThread     *innerThread;\r\n@end\r\n@implementation SAThreadHelper\r\n- (instancetype)init\r\n{\r\n    if (self = [super init]){\r\n        self.stopped = NO;\r\n        __weak typeof(self) weakself = self;\r\n        self.innerThread = [[_SAInnerThread alloc] initWithBlock:^{\r\n            \/\/ 1\u3001\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684RunLoop\r\n            NSRunLoop *runloop = [NSRunLoop currentRunLoop];\r\n            \/\/ 2\u3001\u7ed9RunLoop\u6dfb\u52a0\u4e00\u4e2aSource\uff0c\u4ee5\u514d\u81ea\u52a8\u9500\u6bc1\r\n            [runloop addPort:NSPort.new forMode:NSDefaultRunLoopMode];\r\n            \/\/ 3\u3001RunLoop\u5faa\u73af\uff0c\u4efb\u52a1\u6267\u884c\u7ed3\u675f\u540e\u5982\u6ca1\u6709\u518d\u6b21\u5f00\u542f\u5219\u7ed3\u675f\r\n            while (weakself && !weakself.isStopped) {\r\n                \/\/ 3.1\u3001\u5f00\u542fRunLoop\uff0c\u5f53\u6ca1\u6709\u4efb\u52a1\u65f6\u81ea\u52a8\u8fdb\u5165\u4f11\u7720\u72b6\u6001\uff1b\r\n                \/\/ 3.2\u3001\u8fdb\u5165\u4f11\u7720\u72b6\u6001\u8868\u660e\u5f53\u524d\u4efb\u52a1\u6ca1\u6709\u7ed3\u675f\uff0c\u7ebf\u7a0b\u5c31\u4e0d\u4f1a\u9500\u6bc1\r\n                [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];\r\n            }\r\n            NSLog(@\"--------RunLoop was stopped...\");\r\n        }];\r\n        [self.innerThread start];\r\n        NSLog(@\"--------RunLoop is Starting...\");\r\n    }\r\n    return self;\r\n}\r\n#pragma mark - \u5916\u90e8\u8c03\u7528\r\n- (void)executeWithBlock:(void (^)(void))block\r\n{\r\n    \/\/ \u5916\u90e8\u8c03\u7528\u65f6\uff0c\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u5df2\u9500\u6bc1\u6216\u6ca1\u6709\u6267\u884c\u5185\u5bb9\u5219\u8fd4\u56de\uff1b\r\n    if (!self.innerThread || !block) return;\r\n    \/\/ \u5728\u521b\u5efa\u7684\u5b50\u7ebf\u7a0b\u4e2d\u6267\u884c\u5916\u90e8\u4efb\u52a1\uff0cwaitUntilDone\u53ef\u8bbe\u7f6e\u4e3aNO\uff0c\u5f02\u6b65\u6267\u884c\uff1b\r\n    [self performSelector:@selector(__threadExecuteWithBlock:) onThread:self.innerThread withObject:block waitUntilDone:NO];\r\n}\r\n- (void)stop\r\n{\r\n    if (!self.innerThread) return;\r\n    \/\/ \u7ed3\u675f\u5f53\u524d\u7ebf\u7a0b\u5de5\u4f5c\uff0c\u53ea\u8981\u505c\u6b62\u5f53\u524d\u7ebf\u7a0b\u4e2d\u7684RunLoop\uff0c\u7ebf\u7a0b\u4e2dRunLoop\u6ca1\u6709\u4efb\u52a1\u53ef\u6267\u884c\u7ebf\u7a0b\u5373\u5c06\u7ed3\u675f\uff1b\r\n    \/\/ \u6ce8\u610f\u8fd9\u91ccwaitUntilDone\u8981\u8bbe\u7f6e\u4e3aYES\uff0c\u540c\u6b65\u8fdb\u884c\uff0c\u56e0\u5f53\u5b50\u7ebf\u7a0b\u4f1a\u548c\u5f53\u524d\u63a7\u5236\u5668\u7684\u4e3b\u7ebf\u7a0b\u6709\u7ebf\u7a0b\u95f4\u901a\u8baf\uff0c\u5982\u679c\u5f02\u6b65\u8fdb\u884c\u4f1a\u56e0\u4e3a\u4e3b\u7ebf\u7a0b\u63d0\u524d\u88ab\u9500\u6bc1\u800c\u51fa\u73b0\u91ce\u6307\u9488\u5f02\u5e38\r\n    [self performSelector:@selector(__threadStop) onThread:self.innerThread withObject:nil waitUntilDone:YES];\r\n    self.innerThread = nil;\r\n}\r\n#pragma mark - \u5185\u90e8\u5b9e\u73b0\r\n- (void)__threadExecuteWithBlock:(void (^)(void))block\r\n{\r\n    if (block) block();\r\n}\r\n- (void)__threadStop\r\n{\r\n    \/\/ \u5728\u5f53\u524d\u7ebf\u7a0b\u6267\u884c\u4e00\u4e2a\u4efb\u52a1\uff0c\u5e76\u4e14\u6267\u884c\u7684\u662f\u505c\u6b62\u5f53\u524dRunLoop\u52a8\u4f5c\r\n    \/\/ \u4e0d\u6267\u884c\u505c\u6b62\u52a8\u4f5c\uff0cRunLoop\u4e5f\u4f1a\u56e0\u4e3a\u6267\u884c\u4e86\u52a8\u4f5c\u800c\u88ab\u5524\u9192\uff0c\u7ed3\u675f\r\n    CFRunLoopStop(CFRunLoopGetCurrent());\r\n}\r\n- (void)dealloc\r\n{\r\n    NSLog(@\"%s\", __func__);\r\n    self.stopped = YES;\r\n    [self stop];\r\n}\r\n@end<\/pre>\n<p>2.2\u3001\u5916\u90e8\u8c03\u7528<\/p>\n<pre lang=\"objc\" line=\"1\">#import \"ViewController.h\"\r\n#import \"SAThreadHelper.h\"\r\n@interface ViewController ()\r\n@property (nonatomic, strong)   SAThreadHelper     *threadHelper;\r\n@end\r\n@implementation ViewController\r\n\r\n- (void)viewDidLoad\r\n{\r\n    [super viewDidLoad];\r\n    self.threadHelper = [SAThreadHelper new];\r\n}\r\n- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event\r\n{\r\n    [self.threadHelper executeWithBlock:^{\r\n        NSLog(@\"%@\", [NSThread currentThread]);\r\n    }];\r\n}\r\n- (void)dealloc\r\n{\r\n    NSLog(@\"%s\", __func__);\r\n}\r\n@end<\/pre>\n<p>2.3\u3001\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n2018-11-24 12:06:14.303814+0800 RunLoop[2704:138157] &#8212;&#8212;&#8211;RunLoop is Starting&#8230;<br \/>\n2018-11-24 12:06:16.289095+0800 RunLoop[2704:138157] -[ViewController dealloc]<br \/>\n2018-11-24 12:06:16.289543+0800 RunLoop[2704:138157] -[SAThreadHelper dealloc]<br \/>\n2018-11-24 12:06:16.290330+0800 RunLoop[2704:144194] &#8212;&#8212;&#8211;RunLoop was stopped&#8230;<br \/>\n2018-11-24 12:06:16.291644+0800 RunLoop[2704:144194] -[_SAInnerThread dealloc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u57fa\u672c\u601d\u8def 1.1\u3001\u521b\u5efa\u4e00\u4e2aHelper\u7c7b\u5bf9NSThread\u7c7b\u8fdb\u884c\u5c01\u88c5\uff1b 1.2\u3001Helper\u7c7b\u5185\u90e8\u5b9e\u73b0\u7ebf\u7a0b\u7684\u521b\u5efa\u3001\u6267\u884c\u3001\u9500\u6bc1\u7b49\u4e00\u7cfb\u5217\u64cd\u4f5c\uff1b 1.3\u3001\u6838\u5fc3\u70b9\u5728\u4e8eRunLoop\u7684\u5faa\u73af\u6267\u884c\uff0c\u8c03\u7528runMode:beforeDate\u65b9\u6cd5\u8ba9\u7ebf\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u518d\u901a\u8fc7while\u5faa\u73af\uff0c\u4f7frunloop\u6267\u884c\u7ed3\u675f\u540e\u518d\u6b21\u5f00\u542f\uff0c\u5faa\u73af\u6761\u4ef6\u5916\u90e8\u63a7\u5236\uff0c\u8fd9\u6837\u5c31\u80fd\u8fbe\u5230\u63a7\u5236runloop\u7684\u751f\u547d\u5468\u671f\uff0c\u4ece\u800c\u4fdd\u5168\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\uff1b 1.4\u3001Helper\u7c7b\u5728\u9500\u6bc1\u65f6\uff0c\u8981\u53ca\u65f6\u66f4\u6539runloop\u7684\u5faa\u73af\u6761\u4ef6\uff0c\u5e76\u5524\u9192\u6216\u505c\u6b62\u5b50\u7ebf\u7a0b\u4e2d\u7684runloop\uff0crunloop\u505c\u6b62\u540e\u7ebf\u7a0b\u7ed3\u675f\uff0c\u5426\u5219\u4e24\u8005\u4f1a\u4e00\u76f4\u5b58\u5728\u5185\u5b58\u5f53\u4e2d\uff0crunloop\u56e0\u4e3a\u6ca1\u6709\u53ca\u65f6\u88ab\u5524\u9192\u800c\u4e00\u76f4\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u7ebf\u7a0b\u56e0\u4e3arunloop\u6ca1\u6709\u505c\u6b62\u800c\u65e0\u6cd5\u91ca\u653e\uff1b 1.5\u3001Helper\u5728\u4f7f\u7528NSThread\u521b\u5efa\u5b50\u7ebf\u7a0b\u65f6\u4e5f\u8981\u6ce8\u610f\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff1b 2\u3001\u4ee3\u7801\u793a\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[191,190,180],"class_list":["post-1413","post","type-post","status-publish","format-standard","hentry","category-project","tag-nsthread","tag-runloop","tag-180"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1413","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1413"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1413\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}