iOS笔记-RunLoop、NSURLConnection(OC)

什么是RunLoop
运行循环
一个线程对应一个RunLoop,主线程的RunLoop默认已经启动,子线程的RunLoop得手动启动(调用run方法)
RunLoop只能选择一个Mode启动,如果当前Mode中没有任何Source(Sources0、Sources1)、Timer,那么就直接退出RunLoop

RunLoop作用
保持程序的持续运行
处理App中的各种事件(比如触摸事件、定时器事件、Selector事件)
节省CPU资源,提高程序性能:该做事时做事,该休息时休息......

模拟RunLoop内部实现
其实它内部就是do-while循环,在这个循环内部不断地处理各种任务(比如Source、Timer、Observer)

void message(int num)
{
printf("执行第%i个任务", num);
}
int main(int argc, const char * argv[]) {
do {
printf("有事做吗? 没事做我休眠了");
int number;
scanf("%i", &number);
message(number);
} while (1);
return 0;
}

获得RunLoop对象
RunLoop对象
NSRunLoop
CFRunLoopRef

Foundation[NSRunLoop currentRunLoop];
// 获得当前线程的RunLoop对象[NSRunLoop mainRunLoop];
// 获得主线程的RunLoop对象
Core FoundationCFRunLoopGetCurrent();
// 获得当前线程的RunLoop对象CFRunLoopGetMain();
// 获得主线程的RunLoop对象

RunLoop结构
CFRunLoopRef对应RunLoop对象
CFRunLoopModeRef代表RunLoop的运行模式, 系统默认注册了5个Mode
NSDefaultRunLoopMode:App的默认Mode,通常主线程是在这个Mode下运行
UITrackingRunLoopMode:界面跟踪 Mode,用于 ScrollView 追踪触摸滑动,保证界面滑动时不受其他 Mode 影响
NSRunLoopCommonModes: 这是一个占位用的Mode,不是一种真正的Mode

CFRunLoopTimerRef是基于时间的触发器
CFRunLoopTimerRef基本上说的就是NSTimer,它受RunLoop的Mode影响

CFRunLoopSourceRef是事件源(输入源)
CFRunLoopObserverRef是观察者,能够监听RunLoop的状态改变

// 1.创建Observer
// 第一个参数:用于分配该observer对象的内存
// 第二个参数:用以设置该observer所要关注的的事件
// 第三个参数:用于标识该observer是在第一次进入run loop时执行, 还是每次进入run loop处理时均执行
// 第四个参数:用于设置该observer的优先级
// 第五个参数: observer监听到事件时的回调block
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
switch(activity)
{
case kCFRunLoopEntry:
NSLog(@"即将进入loop");
break;
case kCFRunLoopBeforeTimers:
NSLog(@"即将处理timers");
break;
case kCFRunLoopBeforeSources:
NSLog(@"即将处理sources");
break;
case kCFRunLoopBeforeWaiting:
NSLog(@"即将进入休眠");
break;
case kCFRunLoopAfterWaiting:
NSLog(@"刚从休眠中唤醒");
break;
case kCFRunLoopExit:
NSLog(@"即将退出loop");
break;
default:
break;
}
});

// 2.添加监听
/*
第一个参数: 给哪个RunLoop添加监听
第二个参数: 需要添加的Observer对象
第三个参数: 在哪种模式下监听
*/
CFRunLoopAddObserver(CFRunLoopGetMain(), observer, kCFRunLoopDefaultMode);

// 3,释放observer
CFRelease(observer);

RunLoopRunLoop处理逻辑(略)

RunLoopRunLoop应用
NSTimer
只能在指定的model下运行

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

ImageView显示
只能在指定的model下设置图片

PerformSelector
只能在指定的model下调用

[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:[UIImage imageNamed:@"lnj"] waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];

常驻线程
必须调用run才会执行死循环
NSRunLoop的model中必须有source/timer,死循环才不会退出

NSRunLoop *runloop = [NSRunLoop currentRunLoop];[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
[runloop run]

自动释放池activities = 0x1 = 11:
即将进入RunLoop :
创建一个自动释放池activities = 0xa0 = 160 = 128 + 3232:即将休眠 :
释放上一次的自动释放池, 创建一个新的自动释放池128:即将退出RunLoop :
释放自动释放池

NSURLRequest
用于保存请求地址/请求头/请求体
默认情况下NSURLRequest会自动给我们设置好请求头
request默认情况下就是GET请求

同步请求
如果是调用NSURLConnection的同步方法, 会阻塞当前线程

// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"[http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON](http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON)"];

// 2.根据URL创建NSURLRequest对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// 3.利用NSURLConnection对象发送请求
/*
第一个参数: 需要请求的对象
第二个参数: 服务返回给我们的响应头信息
第三个参数: 错误信息
返回值: 服务器返回给我们的响应体
*/
NSHTTPURLResponse *response = nil; // 真实类型
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
NSLog(@"response = %@", response.allHeaderFields);

异步请求

// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"[http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON](http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON)"];

// 2.根据URL创建NSURLRequest对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// 3.利用NSURLConnection对象发送请求
/*
第一个参数: 需要请求的对象
第二个参数: 回调block的队列, 决定了block在哪个线程中执行
第三个参数: 回调block
*/
// 注意点: 如果是调用NSURLConnection的同步方法, 会阻塞当前线程
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];

POST方法

// 1.创建一个URL
NSURL *url = [NSURL URLWithString:@"[http://129.168.1.1:31812/login](http://129.168.1.1:31812/login)"];

// 2.根据URL创建NSURLRequest对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 2.1设置请求方式
// 注意: POST一定要大写
request.HTTPMethod = @"POST";
// 2.2设置请求体
// 注意: 如果是给POST请求传递参数: 那么不需要写?号
request.HTTPBody = [@"username=cyx&pwd=123&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];

// 3.利用NSURLConnection对象发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];

请求服务器响应

// 1.创建URL
NSURL *url = [NSURL URLWithString:@"[http://xxx.jpg](http://xxx.jpg)"];

// 2.根据URL创建NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];

// 3.利用NSURLConnection发送请求
/*
// 只要调用alloc/initWithRequest, 系统会自动发送请求
[[NSURLConnection alloc] initWithRequest:request delegate:self];
*/

/*
// startImmediately: 如果传递YES, 系统会自动发送请求; 如果传递NO, 系统不会自动发送请求
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn start];
*/

[NSURLConnection connectionWithRequest:request delegate:self];
  • 代理方法
#pragma mark - NSURLConnectionDataDelegate
/*
只要接收到服务器的响应就会调用
response:响应头
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%s", __func__);
}

/*
接收到服务器返回的数据时调用(该方法可能调用一次或多次)
data: 服务器返回的数据(当前这一次传递给我们的, 并不是总数)
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%s", __func__);
}
/*
接收结束时调用
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s", __func__);
}

/*
请求错误时调用(请求超时)
*/
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s", __func__);
}

中文问题

// 1.创建URL
    NSString *urlStr = @"[http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON](http://129.168.1.1:31812/login2?username=cyx&pwd=123&type=JSON)";
    NSLog(@"转换前:%@", urlStr);
    // 2.对URL进行转码
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

你可能感兴趣的:(iOS笔记-RunLoop、NSURLConnection(OC))