iOS开发笔记_5.线程,HTTP请求,定时器

说起线程,不会陌生了,操作系统课程里已经详细介绍了这个东东,这里就不解释了,想要了解的问问百度或者翻翻书。

线程的创建

总结了昨天的学习,有下面几种创建的方式。

        //第一种
        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 = 0 ; i < 20 ; i++) {
                NSLog(@"多线程 :%d",i);
            }
        }];
        //第五种创建线程队列(线程池 )
        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
        //设置线程池中得并发数
        operationQueue.maxConcurrentOperationCount = 1 ;
    
        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:50];
    
    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:50];
    
    //发送异步请求
    [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:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    
    //获取当前的runloop,线程就停在这里
    [[NSRunLoop currentRunLoop] run];
    
    NSLog(@"after");
}

-(void)timerAction:(NSTimer *) timer
{
    index++;
    NSLog(@"定时器打印");
    
    if (index == 5) {
        [timer invalidate];
    }  
}

实现的功能是每隔1秒钟就执行一次timerAction方法,一共执行五次,五次后打印后就结束

原文地址:https://www.cnblogs.com/iOS-dd/p/3166420.html