iOS Developer

Runloop + runtime + other

2017-06-11  本文已影响36人  LeeDev

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优化

上一篇 下一篇

猜你喜欢

热点阅读