源码0503-02-掌握-RunLoop实践

//  ViewController.m
//  02-掌握-RunLoop实践
#import "ViewController.h"
#import "XMGThread.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/** 线程对象 */
@property (nonatomic, strong) XMGThread *thread;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.thread = [[XMGThread alloc] initWithTarget:self selector:@selector(run) object:nil];
    [self.thread start];
}

- (void)run
{
    NSLog(@"----------run----%@", [NSThread currentThread]);
    
    while (1) {
        [[NSRunLoop currentRunLoop] run];
    }
    
    NSLog(@"---------");
}

/**
 * 这种方式虽然能保住线程的命,但是这条线程就无法处理其他行为(事件)
 */
//- (void)run
//{
//    NSLog(@"----------run----%@", [NSThread currentThread]);
//    
//    while (1); // 当前线程永远在处理这行代码
//    
//    NSLog(@"---------");
//}

//- (void)run
//{
//    NSLog(@"----------run----%@", [NSThread currentThread]);
//    
//    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//    [[NSRunLoop currentRunLoop] run];
//    
//    NSLog(@"---------");
////    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
////    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
//}
???????????????
- (void)test
{
    NSLog(@"----------test----%@", [NSThread currentThread]);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
}

- (void)useImageView
{
    // 只在NSDefaultRunLoopMode模式下显示图片
    [self.imageView performSelector:@selector(setImage:) withObject:[UIImage imageNamed:@"placeholder"] afterDelay:3.0 inModes:@[NSDefaultRunLoopMode]];
}

@end

自定义线程对象

//  XMGThread.h
//  02-掌握-RunLoop实践
#import <Foundation/Foundation.h>

@interface XMGThread : NSThread

@end
//  XMGThread.m
//  02-掌握-RunLoop实践
#import "XMGThread.h"

@implementation XMGThread
- (void)dealloc
{
    NSLog(@"%@ --- dealloc", self);
}
@end

02-掌握-RunLoop实践

//  ViewController.m
//  02-掌握-RunLoop实践
#import "ViewController.h"
#import "XMGThread.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/** 线程对象 */
@property (nonatomic, strong) XMGThread *thread;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.thread = [[XMGThread alloc] initWithTarget:self selector:@selector(execute) object:nil];
    [self.thread start];
}

- (void)execute
{
    while (1) {
        [[NSRunLoop currentRunLoop] run];
    }
}

//- (void)execute
//{
//    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
//    
//    [[NSRunLoop currentRunLoop] run];
//}

//- (void)execute
//{
//    NSLog(@"----------run----%@", [NSThread currentThread]);
//    
//    while (1) {
//        [[NSRunLoop currentRunLoop] run];
//        
//        NSLog(@"--------2222");
//    }
//    
//    NSLog(@"---------");
//}

//- (void)execute
//{
//    NSLog(@"----------run----%@", [NSThread currentThread]);
//    
//    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
//    [[NSRunLoop currentRunLoop] run];
//
//    
//    NSLog(@"---------");
//}

- (void)test
{
    NSLog(@"----------test----%@", [NSThread currentThread]);
}

- (void)test2
{
    NSLog(@"***********test2*******%@", [NSThread currentThread]);
    
    NSLog(@"%@", [NSRunLoop currentRunLoop]);
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self performSelector:@selector(test2) onThread:self.thread withObject:nil waitUntilDone:NO];
}

@end