Skip to content

Commit

Permalink
feat: 完成并添加示例
Browse files Browse the repository at this point in the history
  • Loading branch information
xxg90s committed Apr 15, 2022
1 parent 14fdf76 commit b57d21f
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# XGResidentThread
自定义常驻线程

如何使用:参考Demo

Pod支持 `pod 'XGResidentThread'`


23 changes: 23 additions & 0 deletions XGResidentThread.podspec
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
16 changes: 16 additions & 0 deletions XGResidentThread.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
F8C2239F2809250E0071A0C8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F8C2239E2809250E0071A0C8 /* Assets.xcassets */; };
F8C223A22809250E0071A0C8 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F8C223A02809250E0071A0C8 /* LaunchScreen.storyboard */; };
F8C223A52809250E0071A0C8 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F8C223A42809250E0071A0C8 /* main.m */; };
F8C223AE280925660071A0C8 /* XGResidentThread.m in Sources */ = {isa = PBXBuildFile; fileRef = F8C223AD280925660071A0C8 /* XGResidentThread.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -29,6 +30,8 @@
F8C223A12809250E0071A0C8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
F8C223A32809250E0071A0C8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
F8C223A42809250E0071A0C8 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
F8C223AC280925660071A0C8 /* XGResidentThread.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XGResidentThread.h; sourceTree = "<group>"; };
F8C223AD280925660071A0C8 /* XGResidentThread.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XGResidentThread.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -61,6 +64,7 @@
F8C223912809250E0071A0C8 /* XGResidentThread */ = {
isa = PBXGroup;
children = (
F8C223AB280925580071A0C8 /* XGResidentThread */,
F8C223922809250E0071A0C8 /* AppDelegate.h */,
F8C223932809250E0071A0C8 /* AppDelegate.m */,
F8C223952809250E0071A0C8 /* SceneDelegate.h */,
Expand All @@ -76,6 +80,15 @@
path = XGResidentThread;
sourceTree = "<group>";
};
F8C223AB280925580071A0C8 /* XGResidentThread */ = {
isa = PBXGroup;
children = (
F8C223AC280925660071A0C8 /* XGResidentThread.h */,
F8C223AD280925660071A0C8 /* XGResidentThread.m */,
);
path = XGResidentThread;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -149,6 +162,7 @@
F8C2239A2809250E0071A0C8 /* ViewController.m in Sources */,
F8C223942809250E0071A0C8 /* AppDelegate.m in Sources */,
F8C223A52809250E0071A0C8 /* main.m in Sources */,
F8C223AE280925660071A0C8 /* XGResidentThread.m in Sources */,
F8C223972809250E0071A0C8 /* SceneDelegate.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -302,6 +316,7 @@
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -329,6 +344,7 @@
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
13 changes: 13 additions & 0 deletions XGResidentThread/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@
//

#import "ViewController.h"
#import "XGResidentThread.h"

@interface ViewController ()

@property (nonatomic, strong) XGResidentThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.thread = [[XGResidentThread alloc] init];

[self.thread executeTask:^{
NSLog(@"current resident thread operate = %@", [NSThread currentThread]);
}];

[XGResidentThread executeTask:^{
NSLog(@"global resident thread operate = %@", [NSThread currentThread]);
} threadIdentity:@"test"];
}


Expand Down
29 changes: 29 additions & 0 deletions XGResidentThread/XGResidentThread/XGResidentThread.h
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
101 changes: 101 additions & 0 deletions XGResidentThread/XGResidentThread/XGResidentThread.m
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

0 comments on commit b57d21f

Please sign in to comment.