Runloop应用实例
AFNetworking
AFURLConnectionOperation 这个类是基于 NSURLConnection
构建的,其希望能在后台线程接收 Delegate 回调。为此 AFNetworking 单独创建了一个线程,并在这个线程中启动了一个 RunLoop:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
+ (void)networkRequestThreadEntryPoint:(id)__unused object { @autoreleasepool { [[NSThread currentThread] setName:@"AFNetworking"]; NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode]; [runLoop run]; }} + (NSThread *)networkRequestThread { static NSThread *_networkRequestThread = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; [_networkRequestThread start]; }); return _networkRequestThread;} |
RunLoop 启动前内部必须要有至少一个 Timer/Observer/Source,所以 AFNetworking 在 [runLoop run] 之前先创建了一个新的 NSMachPort 添加进去了。通常情况下,调用者需要持有这个 NSMachPort (mach_port) 并在外部线程通过这个 port 发送消息到 loop 内;但此处添加 port 只是为了让 RunLoop 不至于退出,并没有用于实际的发送消息。
|
1
2
3
4
5
6
7
8
9
10
|
- (void)start { [self.lock lock]; if ([self isCancelled]) { [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; } else if ([self isReady]) { self.state = AFOperationExecutingState; [self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; } [self.lock unlock];} |
当需要这个后台线程执行任务时,AFNetworking 通过调用 [NSObject performSelector:onThread:..] 将这个任务扔到了后台线程的 RunLoop 中。
Runloop应用实例的更多相关文章
- iOS Run loop使用实例
http://blog.csdn.net/libaineu2004/article/details/45364737 一.Runloop简介: Run loops 是线程相关的的基础框架的一部分.一个 ...
- iOS开发 - 啰嗦讲解 Runloop
写在前面的 为什么要了解 RunLoop?如果你想成为一个高级iOS开发工程师,那这是你必须了解的东西,他能帮助你更好的理解底层实现的原理,可以利用它的特性做出一些高效又神奇的功能.RunLoop这个 ...
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- RunLoop和autorelease的一道面试题
有这么一道iOS面试题 以下代码有没有什么问题?如果有?如何解决? for (int i = 0; i < largeNumber; i++) { NSString *str = [NSStri ...
- RunLoop(官方文档翻译)
循环运行 运行循环是与线程相关联的基本基础设施的一部分.一个运行循环是用于调度工作,并协调接收传入事件的事件处理循环.一个运行循环的目的是让你的线程繁忙时,有工作要做,把你的线程时有没有睡觉. 循环运 ...
- RunLoop机制理解
一.浅识RunLoop RunLoop在开发中我们一直在用,但是没有注意他.要想理解RunLoop,首先我们需要先了解一下程序运行机制. 程序运行机制:我们都知道OC是运行时语言,也就是说对象的类型是 ...
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子.游戏中有一个计时功能在主线程中调用: 1 + (NSTimer *)scheduledTimerWithTimeInterval:( ...
- 实例化讲解 RunLoop
实例化讲解RunLoop 之前看过很多有关RunLoop的文章,其中要么是主要介绍RunLoop的基本概念,要么是主要讲解RunLoop的底层原理,很少用真正的实例来讲解RunLoop的,这其中有大部 ...
- 关于 IOS Runtime Runloop 2
Runtime 也就是运行时组件,一个纯C语言写的基础库. 我们平时编写的OC代码中, 程序运行过程时, 其实最终都是转成了runtime的C语言代码 Objective-C编写出来的程序必须得到ru ...
随机推荐
- VMWare虚拟机下为Ubuntu 12.04.1配置静态IP_转
转自:http://www.cnblogs.com/objectorl/archive/2012/09/27/vmware-ubuntu-nat-static-ip-settings.html 背景在 ...
- mysql 模块使用
import MySQLdb conn = MySQLdb.connect(host=',db='fengjian') cur = conn.cursor() sql = 'insert into a ...
- [转][C++ 11]override and final - write clean and maintainable C++ code
原文: http://arne-mertz.de/2015/12/modern-c-features-override-and-final/ Today I write about a pair of ...
- Javascript中Number()、parseIn()和parseFloat()的区别
有3个函数可以把非数值转化成数值:Number().parseInt()和parseFloat().第一个函数,即转型函数Number()可以用于任何数据类型,而另两个函数则专门用于把字符串转换成数值 ...
- nginx地址重写
1. 域名重定向 server_name wx.he.com weixin.ha.com; if ($http_host !~* "wx\.he\.com"){ rewrite ^ ...
- Java基础之一组有用的类——生成日期和时间(TryDateFormats)
控制台程序. java.util包中含有相当多的类涉及日期和时间,包括Date类.Calendar类和GregorianCalendar类. Date类对象其实定义了精确到毫秒的时刻,从1970年1月 ...
- 怎么设置task的最大线程数
//-------------------------------------------------------------------------- // // Copyright (c) Mic ...
- redhat linux 安装mysql5.6.27
1.yum安装mysql(root身份) yum install mysql-server mysql-devel mysql -y 如没有配置yum,请参见博客:http://www.cnblogs ...
- Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- .net 反编译工具
遇到一个需求,做一个专门访问自己网站的浏览器给用户使用,这个浏览器提供登录功能.此时是一个安装在客户端的exe程序,做登录验证要用到webservice,不能将验证逻辑写入exe中,否则客户端可以利用 ...