iOS开发笔记_5.线程,HTTP请求,定时器
2024-08-26 12:22:58
说起线程,不会陌生了,操作系统课程里已经详细介绍了这个东东,这里就不解释了,想要了解的问问百度或者翻翻书。
线程的创建
总结了昨天的学习,有下面几种创建的方式。
//第一种
NSThread *t = [[NSThread alloc] initWithTarget:self selector:
@selector(mutitly) object:nil];
[t start];
//第二种
[NSThread detachNewThreadSelector:@selector(mutitly) toTarget:self withObject:nil];
//第三种,这种比较常用
[self performSelectorInBackground:@selector(mutitly) withObject:nil];
//第四种
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperationWithBlock:^{ for (int i = ; i < ; i++) {
NSLog(@"多线程 :%d",i);
}
}];
//第五种创建线程队列(线程池 )
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//设置线程池中得并发数
operationQueue.maxConcurrentOperationCount = ; NSInvocationOperation *invocation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread1) object:nil];
[invocation1 setQueuePriority:NSOperationQueuePriorityVeryLow]; NSInvocationOperation *invocation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(thread2) object:nil];
[invocation2 setQueuePriority:NSOperationQueuePriorityVeryHigh]; [operationQueue addOperation:invocation1];
[operationQueue addOperation:invocation2];
这个例子是在做实验的时候总结的
-(void)setImageWithURL:(NSURL *) url{ //加载网络图片的时候应该开启一个线程,不然加载的时候会阻塞
[self performSelectorInBackground:@selector(loadData:) withObject:url]; //不推荐使用这种方法调用,因为这时候按钮会卡住
//[self loadData:url];
} -(void)loadData:(NSURL *) url{ @autoreleasepool {
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data]; //在主线程上执行setImage方法
[self performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
}
}
应该注意的是,加载网络数据的时候,最好要开启一个新的线程,并且,要把网络上得图片加载到本地的话,应该在主线程上调用,这里调用的是performSelectorOnMainThread:方法,这个很重要,有关UI的操作都是在主线程上进行的。
HTTP请求
用户发送请求的时候有多种请求方式,比较常见的请求方式有两种
1、“get”请求:向服务器索取数据
2、“post”请求,向服务器提交数据,如“用户登录”等等。
同步请求(只能等服务器的数据完全返回响应后,程序才往下执行)
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //设置请求方式
[request setHTTPMethod:@"GET"];
//设置请求时间
[request setTimeoutInterval:]; NSURLResponse *respon ;
NSError *error = nil; //发送一个同步请求
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&respon error:&error]; NSInteger statusCode = [respon statusCode];
NSLog(@"响应码:%d",statusCode); NSDictionary *headFields = [respon allHeaderFields];
NSLog(@"响应头:%@",headFields); NSString *htmlstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",htmlstring);
异步请求(不用等服务器完全响应,程序边执行边加载数据)
- (IBAction)asynchAction:(id)sender { NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //设置请求方式
[request setHTTPMethod:@"GET"];
//设置请求时间
[request setTimeoutInterval:]; //发送异步请求
[NSURLConnection connectionWithRequest:request delegate:self]; //第二种异步请求方式,用的不是很多
// NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
//
// [NSURLConnection sendAsynchronousRequest:request queue:operationQueue completionHandler:^(NSURLResponse *respon, NSData *data, NSError *error) {
// if([NSThread isMultiThreaded]){
// NSLog(@"是多线程");
// }
//
// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@",string);
//
// }];
主要是要知道url,request的创建,还有请求的方式是同步还是异步。
总结一下访问网络的基本流程。
1、构造NSURL实例(地址)
2、生成NSURLRequest(请求)
3、通过NSURLConnection发送请求
4、通过NSURLRespond实例和NSError实例分析结果
5、接收返回的数据
还有几个比较常用的协议方法,NSURLConnectionDataDelegate
//服务器响应的方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSDictionary *allHeadFields = [httpResponse allHeaderFields];
NSLog(@"%@",allHeadFields);
}
//接受数据的方法,会调用多次,每一次调用服务器的一部分数据data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [resultData appendData:data]; }
//数据加载完成调用的方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSString *htmlString = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
NSLog(@"%@",htmlString); }
定时器
-(void)mutiThread{
//多线程里面开启一个定时器可以提高精确度
//启动定时器
NSLog(@"定时器启动");
@autoreleasepool {
[NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
} //获取当前的runloop,线程就停在这里
[[NSRunLoop currentRunLoop] run]; NSLog(@"after");
} -(void)timerAction:(NSTimer *) timer
{
index++;
NSLog(@"定时器打印"); if (index == ) {
[timer invalidate];
}
}
实现的功能是每隔1秒钟就执行一次timerAction方法,一共执行五次,五次后打印后就结束
最新文章
- Oracle 11g静默安装软件+手工创建数据库
- Linux网络栈下两层实现
- Windows桌面共享中一些常见的抓屏技术
- Scalaz(43)- 总结 :FP就是实用的编程模式
- Mysql之INFORMATION_SCHEMA解析1
- (四)学习CSS之position、bottom、left、right和top属性
- 关于MySQL的各种总结
- Educational Codeforces Round 15_B. Powers of Two
- Python+Selenium 环境配置之Firefox,IE,Chrome几种浏览器运行
- myeclipse 与 mysql 的连接
- Java Web之Servlet的三大作用域对象
- 一.rest-framework之版本控制 二、Django缓存 三、跨域问题 四、drf分页器 五、响应器 六、url控制器
- Pony 编程语言介绍
- Proxy --概述篇
- JMeter 各组件介绍以及用法
- RecyclerView的点击事件添加-------接口回调的形式添加
- java基础-day1
- `Vue`中为什么访问不了以`$`和`_`开头的属性?
- Semaphore-信号灯机制
- Qt::WA_DeleteOnClose 造成的野指针问题