-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xxg90s
committed
Apr 15, 2022
1 parent
14fdf76
commit b57d21f
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# XGResidentThread | ||
自定义常驻线程 | ||
|
||
如何使用:参考Demo | ||
|
||
Pod支持 `pod 'XGResidentThread'` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# | ||
# Be sure to run `pod spec lint XGResidentThread.podspec' to ensure this is a | ||
# valid spec and to remove all comments including this before submitting the spec. | ||
# | ||
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html | ||
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ | ||
# | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "XGResidentThread" | ||
s.version = "1.0.0" | ||
s.license = "MIT" | ||
s.summary = "自定义全局常驻线程及实现常驻线程并管理其生命周期" | ||
s.homepage = "https://github.com/xxg90s/XGResidentThread" | ||
s.source = { :git => "https://github.com/xxg90s/XGResidentThread.git", :tag => "#{s.version}" } | ||
s.source_files = "XGResidentThread/XGResidentThread/XGResidentThread/*.{h,m}" | ||
s.requires_arc = true | ||
s.platform = :ios, "10.0" | ||
s.frameworks = "UIKit", "Foundation" | ||
s.author = { "xxg90s" => "[email protected]" } | ||
s.social_media_url = "https://github.com/xxg90s" | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// XGResidentThread.h | ||
// XGResidentThread | ||
// | ||
// Created by xxg90s on 2022/4/15. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
typedef void (^XGResidentThreadTask)(void); | ||
|
||
@interface XGResidentThread : NSObject | ||
|
||
/// Execute operations in a global resident thread | ||
/// @param task operations | ||
/// @param threadIdentity global resident thread key | ||
+ (void)executeTask:(XGResidentThreadTask)task threadIdentity:(NSString *)threadIdentity; | ||
|
||
/// Execute task | ||
- (void)executeTask:(XGResidentThreadTask)task; | ||
|
||
/// Stop thread | ||
- (void)stop; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// XGResidentThread.m | ||
// XGResidentThread | ||
// | ||
// Created by xxg90s on 2022/4/15. | ||
// | ||
|
||
#import "XGResidentThread.h" | ||
|
||
@interface XGResidentThread() | ||
|
||
@property (nonatomic, strong) NSThread *innerThread; | ||
//@property (nonatomic, assign, getter=isStopped) BOOL stopped; | ||
|
||
@end | ||
|
||
@implementation XGResidentThread | ||
|
||
static NSMutableDictionary *xg_threadDictionary; | ||
|
||
+ (void)executeTask:(XGResidentThreadTask)task threadIdentity:(nonnull NSString *)threadIdentity { | ||
if (!task || !threadIdentity || threadIdentity.length == 0) { | ||
return; | ||
} | ||
if (!xg_threadDictionary) { | ||
xg_threadDictionary = [[NSMutableDictionary alloc] init]; | ||
} | ||
|
||
XGResidentThread *specifyThread = [xg_threadDictionary valueForKey:threadIdentity]; | ||
|
||
if (!specifyThread) { | ||
specifyThread = [[XGResidentThread alloc] init]; | ||
[specifyThread __setInnerThreadName:threadIdentity]; | ||
|
||
[xg_threadDictionary setValue:specifyThread forKey:threadIdentity]; | ||
} | ||
|
||
[specifyThread executeTask:task]; | ||
} | ||
|
||
|
||
- (instancetype)init { | ||
if (self = [super init]) { | ||
// _stopped = NO; | ||
|
||
// __weak typeof(self) weakSelf = self; | ||
_innerThread = [[NSThread alloc] initWithBlock:^{ | ||
// [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode]; | ||
// while (weakSelf && !weakSelf.isStopped) { | ||
// [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; | ||
// } | ||
|
||
CFRunLoopSourceContext context = {0}; | ||
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); | ||
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode); | ||
CFRelease(source); | ||
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, false); | ||
}]; | ||
[_innerThread start]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)executeTask:(XGResidentThreadTask)task { | ||
if (!self.innerThread || !task) { | ||
return; | ||
} | ||
|
||
[self performSelector:@selector(__executeTask:) onThread:self.innerThread withObject:task waitUntilDone:NO]; | ||
} | ||
|
||
- (void)stop { | ||
if (!self.innerThread) { | ||
return; | ||
} | ||
|
||
// execute after all tasks are executed | ||
[self performSelector:@selector(__stop) onThread:self.innerThread withObject:nil waitUntilDone:YES]; | ||
} | ||
|
||
- (void)dealloc { | ||
[self stop]; | ||
} | ||
|
||
#pragma mark - private methods | ||
- (void)__setInnerThreadName:(NSString *)name { | ||
self.innerThread.name = name; | ||
} | ||
- (void)__stop { | ||
// self.stopped = YES; | ||
CFRunLoopStop(CFRunLoopGetCurrent()); | ||
self.innerThread = nil; | ||
} | ||
|
||
- (void)__executeTask:(XGResidentThreadTask)task { | ||
@autoreleasepool { | ||
task(); | ||
} | ||
} | ||
|
||
@end |