iOS开发----UI部分----如何启动和停止RunLoop

#import "ViewController.h"

 @interface ViewController ()<NSURLConnectionDataDelegate>

@property (nonatomic, assign) CFRunLoopRef runLoop;// 保持同一个线程 C语言

@end

 @implementation ViewController

 - (void)viewDidLoad {

    [super viewDidLoad];

      dispatch_async(dispatch_get_global_queue(0, 0), ^{

       NSURLConnection *conn =  [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:@""] delegate:self];

        //决定代理在哪个队列执行,

       //  [conn setDelegateQueue:[[NSOperationQueue alloc] init]];

        // 放在子线程, 子线程的RunLoop,默认是不开启的,不开启就无法时刻监听  网络下载请求的事件

        // 子线程要手动的启动RunLoop

        // [[NSRunLoop currentRunLoop] run];

        self.runLoop = CFRunLoopGetCurrent(); // 获得子线程的runloop

        CFRunLoopRun(); // 启动

    });

}

#pragma mark-<NSURLConnectionDataDelegate>

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    CFRunLoopStop(self.runLoop);// 关闭对应的runLoop

}

@end

原文地址:https://www.cnblogs.com/1018475062qq/p/7020448.html