Runloop + runtime + other

weixin_34007886發表於2017-06-11

1.Runloop

Runloop 應用1

執行緒 保活,讓某個執行緒一直存在,讓一些操作只在這個執行緒中去執行

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self threadTest];
}

- (void)threadTest {
    
    HLThread *subThread = [[HLThread alloc] initWithTarget:self selector:@selector(subThreadEntryPoint) object:nil];
    [subThread start];
    self.subThread = subThread;
}

//啟動runloop,把執行緒subThread 一直在保留著
- (void)subThreadEntryPoint {
    
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //如果註釋了下面這一行,子執行緒中的任務並不能正常執行
    [runLoop addPort:[NSMachPort port] forMode:NSRunLoopCommonModes];
    NSLog(@"啟動RunLoop前--%@",runLoop.currentMode);
    [runLoop run];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self performSelector:@selector(subThreadOpetion) onThread:self.subThread withObject:nil waitUntilDone:NO];
}

- (void)subThreadOpetion {
    
    NSLog(@"%@----子執行緒任務開始",[NSThread currentThread]);
    [NSThread sleepForTimeInterval:3.0];
    NSLog(@"%@----子執行緒任務結束",[NSThread currentThread]);
}

2.runtime

1.http://tech.glowing.com/cn/objective-c-runtime/
2.http://yulingtianxia.com/blog/2014/11/05/objective-c-runtime/

sendMsg(obj, foo) 執行過程
1.通過 obj的isa 指標找到 class
2.在class的method list 方法中去找 foo,如果沒有找到就去superclass中找
2‘. 在class中有個objc_cache,如果找到了會把 foo的imp快取起來
3.一旦找到了foo,就執行 imp

轉發機制
1.Method resolution
2.Fast forwarding
3.Normal forwarding(簽名+再次轉發)

下面通過程式碼來把轉發機制說明白
我的想法有個Person
1.當person 執行 某個方法,假設我們沒有實現person 的方法,那麼我們可以去通過 + (BOOL)resolveInstanceMethod:(SEL)sel 來捕獲這個沒有實現的方法,並實現就好了
2.如果我們不想實現當前方法,想指定某個物件Machine 去實現,那麼就直接呼叫 - (id)forwardingTargetForSelector:(SEL)aSelector 快速轉給 Machine 物件去實現
3.如果這個時候 沒有指定,那麼就需要 簽名+ 再次轉發, - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector- (void)forwardInvocation:(NSInvocation *)anInvocation,這裡我是定義了Machine2物件來處理

下面是程式碼
Person

.h 

#import <Foundation/Foundation.h>

@interface Person : NSObject

//方法解析
- (void)eat:(NSString *)foot;
+ (void)learn:(NSString *)someString;

//快速 方法轉發
- (void)print;//把列印方法轉給machine 物件去執行
+ (void)printClass;
//normal 方法轉發

- (void)print2;//轉發給machine2 去操作


@end

.m
#import "Person.h"
#import <objc/runtime.h>
#import "Machine.h"
#import "Machine2.h"



@interface Person ()

@property (nonatomic,strong) Machine2 *forward;
@end
@implementation Person

/* 這裡不去實現person 的兩個方法**/

-(id)init {
    if (self = [super init]) {
        _forward = [Machine2 new];
    }
    return self;
}

//例項方法
+ (BOOL)resolveInstanceMethod:(SEL)sel {
    
    if(sel == @selector(eat:)) {
        //把 eat:方法加入到 class 中,並實現 imp 方法
        class_addMethod([self class], sel,class_getMethodImplementation([self class], @selector(instanceMethod:)), "v@:");
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

- (void)instanceMethod:(NSString *)some {
    
    NSLog(@"例項方法解析成功 :%@",some);
}

//類方法轉發

+ (BOOL)resolveClassMethod:(SEL)sel {
    
    if(sel == @selector(learn:)) {
        
//        class_addMethod([self class], sel,class_getMethodImplementation([self class], @selector(instanceClassMethod:)), "v@:");
        class_addMethod(object_getClass(self), sel,class_getMethodImplementation(object_getClass(self), @selector(instanceClassMethod:)), "v@:");
        return YES;
    }
    return [super resolveClassMethod:sel];
}

+ (void)instanceClassMethod:(NSString *)some {
    
    NSLog(@"類方法解析成功 :%@",some);
}



/** -----------------forwardingTargetForSelector-----------------**/

//快速轉發(Fast forwarding)
- (id)forwardingTargetForSelector:(SEL)aSelector {
    
    if(aSelector == @selector(print)) {
        
        return [[Machine alloc]init];
    }
    return [super forwardingTargetForSelector:aSelector];
}

+ (id)forwardingTargetForSelector:(SEL)aSelector {
    if(aSelector == @selector(printClass)) {
        return NSClassFromString(@"Machine");
    }
    return [super forwardingTargetForSelector:aSelector];
}

//Normal forwarding
//NSInvocation 實際上就是對一個訊息的描述,包括selector 以及引數等資訊

//先進行方法簽名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    
    NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
    if(!signature) {
        
        signature = [_forward methodSignatureForSelector:aSelector];
    }
    return signature;
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    
    [anInvocation invokeWithTarget:_forward];
}








@end


Machine

#import "Machine.h"

@implementation Machine

//方法轉發
- (void)print {//把列印方法轉給machine 物件去執行
    
    NSLog(@"Machine action print");
}
+ (void)printClass {
    
    NSLog(@"Machine action printClass");
}

@end

Machine2


#import "Machine2.h"

@implementation Machine2

- (void)print2 {//把列印方法轉給machine 物件去執行
    
    NSLog(@"Machine2 action print");
}

@end

程式碼執行demo

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    [Person learn:@"book"];
    [Person printClass];
    
    Person * p = [Person new];
    [p eat:@"foot"];
    [p print];
    
    [p print2];
    
}

思考:想這些轉發機制,我們就可以通過一個統一的物件A,來實現各種介面,然後在A裡面根據方法名 來指派給 另外一個物件(B,C 等)去實現,這樣外界就不需要知道B,C 等 這些物件

3.other

scrollView優化

相關文章