{"id":1446,"date":"2018-11-30T11:06:43","date_gmt":"2018-11-30T03:06:43","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1446"},"modified":"2020-12-01T12:44:41","modified_gmt":"2020-12-01T04:44:41","slug":"%e4%bd%bf%e7%94%a8gcd%e5%b0%81%e8%a3%85%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e5%ae%9a%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2018\/11\/30\/1106431446.html","title":{"rendered":"\u4f7f\u7528GCD\u5c01\u88c5\u4e00\u4e2a\u7b80\u5355\u7684\u5b9a\u65f6\u5668"},"content":{"rendered":"<p>1\u3001GCD\u5b9a\u65f6\u5668\u5b9e\u73b0\u7684\u57fa\u7840\u4ee3\u7801<\/p>\n<pre lang=\"objc\" line=\"1\">dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, <#dispatchQueue#>);\r\ndispatch_source_set_timer(timer, DISPATCH_TIME_NOW, <#intervalInSeconds#> * NSEC_PER_SEC, <#leewayInSeconds#> * NSEC_PER_SEC);\r\ndispatch_source_set_event_handler(timer, ^{\r\n    <#code to be executed when timer fires#>\r\n});\r\ndispatch_resume(timer);<\/pre>\n<p>2\u3001\u5bf9\u5176\u8fdb\u884c\u7b80\u5355\u5c01\u88c5\uff0c\u4f7f\u5916\u90e8\u8c03\u7528\u65f6\u4f20\u5165\u95f4\u9694\u65f6\u95f4\u3001\u91cd\u590d\u6b21\u6570\u3001\u5b9a\u65f6\u6267\u884c\u4ee3\u7801\u5373\u53ef\u81ea\u52a8\u5b9a\u65f6\u6267\u884c<\/p>\n<pre lang=\"objc\" line=\"1\">#import <Foundation\/Foundation.h>\r\n\r\nNS_ASSUME_NONNULL_BEGIN\r\n\r\n@interface SATimer : NSObject\r\n\r\n\/**\r\n \u521b\u5efa\u5e76\u5f00\u542f\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\r\n\r\n @param interval \u91cd\u590d\u6267\u884c\u7684\u95f4\u9694\u65f6\u95f4\r\n @param count \u91cd\u590d\u6b21\u6570\r\n @param block \u91cd\u590d\u6267\u884c\u4ee3\u7801\r\n @return \u8fd4\u56de\u5b9a\u65f6\u5668\u7684\u552f\u4e00\u6807\u8bc6\r\n *\/\r\n+ (NSString *)timerWithInterval:(NSTimeInterval)interval repeatCount:(NSUInteger)count block:(void (^)(NSUInteger remainCount))block;\r\n\r\n\/**\r\n \u6839\u636e\u5b9a\u65f6\u5668\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u505c\u6b62\u5b9a\u65f6\u5668\r\n\r\n @param timerId \u5b9a\u65f6\u5668\u552f\u4e00\u6807\u8bc6\r\n *\/\r\n+ (void)invalidateWithId:(NSString *)timerId;\r\n@end\r\n\r\nNS_ASSUME_NONNULL_END<\/pre>\n<pre lang=\"objc\" line=\"1\">#import \"SATimer.h\"\r\nstatic NSMutableDictionary  *timerDictionary_;\r\nstatic NSMutableDictionary  *countDictionary_;\r\nstatic dispatch_semaphore_t semaphore_;\r\n@implementation SATimer\r\n+ (void)initialize\r\n{\r\n    static dispatch_once_t onceToken;\r\n    dispatch_once(&onceToken, ^{\r\n        semaphore_ = dispatch_semaphore_create(1);\r\n        timerDictionary_ = [NSMutableDictionary dictionary];\r\n        countDictionary_ = [NSMutableDictionary dictionary];\r\n    });\r\n}\r\n+ (NSString *)timerWithInterval:(NSTimeInterval)interval repeatCount:(NSUInteger)count block:(void (^)(NSUInteger remainCount))block;\r\n{\r\n    if ((block == nil) || (count <= 0) || (interval <= 0)) return nil;\r\n    \/\/ gcd\u5b9a\u65f6\u5668\u57fa\u672c\u5b9e\u73b0\r\n    dispatch_queue_t queue = dispatch_queue_create(\"com.yusian.timer-queue\", DISPATCH_QUEUE_CONCURRENT);\r\n    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);\r\n    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, interval * NSEC_PER_SEC, 0 * NSEC_PER_SEC);\r\n    NSString *timerId = [NSString stringWithFormat:@\"%p\", timer];\r\n    \r\n    \/\/ \u5b57\u5178\u5199\u5165\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\r\n    dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER);\r\n    [timerDictionary_ setObject:timer forKey:timerId];\r\n    [countDictionary_ setObject:@(count) forKey:timerId];\r\n    dispatch_semaphore_signal(semaphore_);\r\n    \/\/ \u6267\u884c\u5b9a\u65f6\u4efb\u52a1\r\n    dispatch_source_set_event_handler(timer, ^{\r\n        NSNumber *count = [countDictionary_ objectForKey:timerId];\r\n        NSUInteger remainCount = count.unsignedIntegerValue;\r\n        block(--remainCount);\r\n        \/\/ \u5b57\u5178\u5199\u5165\u65f6\u7684\u7ebf\u7a0b\u5b89\u5168\r\n        dispatch_semaphore_wait(semaphore_, DISPATCH_TIME_FOREVER);\r\n        [countDictionary_ setObject:@(remainCount) forKey:timerId];\r\n        dispatch_semaphore_signal(semaphore_);\r\n        \/\/ \u91cd\u590d\u6b21\u6570\u5269\u4f59\u91cf\u4e3a0\u65f6\u81ea\u52a8\u9000\u51fa\r\n        if (remainCount == 0) [self invalidateWithId:timerId];\r\n    });\r\n    dispatch_resume(timer);\r\n    return timerId;\r\n}\r\n\r\n+ (void)invalidateWithId:(NSString *)timerId\r\n{\r\n    dispatch_source_t timer = [timerDictionary_ objectForKey:timerId];\r\n    if (timer == nil) return;\r\n    dispatch_source_cancel(timer);\r\n    [timerDictionary_ removeObjectForKey:timerId];\r\n    [countDictionary_ removeObjectForKey:timerId];\r\n    NSLog(@\"SATimer invalidate - %@\", timerId);\r\n}\r\n\r\n@end<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001GCD\u5b9a\u65f6\u5668\u5b9e\u73b0\u7684\u57fa\u7840\u4ee3\u7801 dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, ); dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, * NSEC_PER_SEC, * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ }); dispatch_resume(timer); 2\u3001\u5bf9\u5176\u8fdb\u884c\u7b80\u5355\u5c01\u88c5\uff0c\u4f7f\u5916\u90e8\u8c03\u7528\u65f6\u4f20\u5165\u95f4\u9694\u65f6\u95f4\u3001\u91cd\u590d\u6b21\u6570\u3001\u5b9a\u65f6\u6267\u884c\u4ee3\u7801\u5373\u53ef\u81ea\u52a8\u5b9a\u65f6\u6267\u884c #import NS_ASSUME_NONNULL_BEGIN @interface SATimer : NSObject \/** \u521b\u5efa\u5e76\u5f00\u542f\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1 @param interval \u91cd\u590d\u6267\u884c\u7684\u95f4\u9694\u65f6\u95f4 @param count \u91cd\u590d\u6b21\u6570 @param block \u91cd\u590d\u6267\u884c\u4ee3\u7801 @return \u8fd4\u56de\u5b9a\u65f6\u5668\u7684\u552f\u4e00\u6807\u8bc6 *\/ + (NSString *)timerWithInterval:(NSTimeInterval)interval repeatCount:(NSUInteger)count block:(void (^)(NSUInteger remainCount))block; \/** \u6839\u636e\u5b9a\u65f6\u5668\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u505c\u6b62\u5b9a\u65f6\u5668 @param timerId \u5b9a\u65f6\u5668\u552f\u4e00\u6807\u8bc6 *\/ + (void)invalidateWithId:(NSString *)timerId; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,11],"tags":[78,201,200,180],"class_list":["post-1446","post","type-post","status-publish","format-standard","hentry","category-cpp","category-project","tag-gcd","tag-nstimer","tag-200","tag-180"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1446","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=1446"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1446\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}