多线程的实现:NSThread

1.子线程的创建:两种方法

第一种: [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];

第二种:NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];

[thread start];

差异:第一种直接方便,第二种在启动前可以对其的stack等参数配置,更具灵活性。

2.在子线程中召唤主线程做事:

[self performSelectorOnMainThread:@selector(函数名) withObject:nil waitUntilDone:YES];

//如果waitUntilDone参数为YES,那么当前线程会被阻拦,直到selector运行完。

3.代码简单示例一:登录

//此为多线程实现登录放UI卡死的代码
//成功进行登录验证后进入到下一ViewController
-(void)presentToNextview{
//到下一界面
} //登录验证
-(void)loginCheck{
//包含POST或GET请求来完成数据的验证,验证成功就跳转到下一界面
if(账号密码匹配){
//召唤主线程跳转到下一界面
[self performSelectorOnMainThread:@selector(presentToNextview) withObject:nil waitUntilDone:YES];
}
} -(void)showindicator{
//显示登录时转圈圈的菊花
} //登录按钮的点击事件
-(IBAction)loginBTN:(id)sender{
//执行的函数有:
[self showindicator]; //开辟新的线程,执行需要联网耗时的函数loginCheck
[NSThread detachNewThreadSelector:@selector(loginCheck) toTarget:self withObject:nil];
}

4.代码示例二:显示一张网上的图片,源代码:

DownloadViewController.h

#import <UIKit/UIKit.h>
#import "FileConnection.h"
@interface DownloadViewController : UIViewController @end

DownloadViewController.m

#import "DownloadViewController.h"
#define kURL @"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d37ccb6e0e950a304e241f58ca.jpg"
@interface DownloadViewController () @property UIImageView *imageView; @end UIActivityIndicatorView* activityIndicatorView; @implementation DownloadViewController -(void)downloadImage:(NSString *) url{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
NSLog(@"没有图片");
}else{
NSLog(@"刷新图片");
[ activityIndicatorView stopAnimating ];//停止
//通知主线程做事
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:NO];
//如果waitUntilDone参数为YES,那么当前线程会被阻拦,直到selector运行完。
}
} -(void)updateUI:(UIImage*) image{
self.imageView.image = image;
//sleep(5);
} -(void)showindicatior{
activityIndicatorView = [ [ UIActivityIndicatorView alloc ] initWithFrame:CGRectMake(,,30.0,30.0)];
activityIndicatorView.activityIndicatorViewStyle= UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];//启动 }
- (void)viewDidLoad
{
[super viewDidLoad]; self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; [self.view addSubview:self.imageView]; //显示菊花
[self showindicatior]; //开辟一个新的线程 2种方法
[NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
//NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];
//[thread start];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

[IOS多线程]的使用:防止进行HTTP数据请求时,UI卡死。的更多相关文章

  1. 当客户端提交更新数据请求时,是先写入edits,然后再写入内存的

    http://blog.sina.com.cn/s/blog_6f83c7470101b7d3.html http://blog.csdn.net/slq1023/article/details/49 ...

  2. iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

    NSURL进行数据请求(POST与GET)   使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以 ...

  3. 当对服务器端返回的极光推送数据请求时,AFN 的 GET 请求失败如何解决

    代码段 控制台   只需在 manager 那里添加一行代码即可 //传入json格式数据,不写则普通post     manager.requestSerializer = [AFJSONReque ...

  4. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

    正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...

  5. VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

     created() {     var that=this     axios.get('http://jsonplaceholder.typicode.com/todos')     .then( ...

  6. bootstrap table分页,重新数据查询时页码为当前页问题

    问题描述: 使用bootstrap table时遇到一个小问题,第一次查询数据未5页,翻页到第5页后,选中条件再次查询数据时,传到后端页码仍旧为5,而此时数据量小于5页,表格显示为未查询到数据. 处理 ...

  7. iOS多线程主题

    下面是:2个并发进程.和2个并发线程的示意图: 下面介绍三种多线程技术(Thread.Cocoa Operation.Grand Central Dispatch): 1.最轻量级Thread(需要自 ...

  8. iOS多线程技术方案

    iOS多线程技术方案 目录 一.多线程简介 1.多线程的由来 2.耗时操作的模拟试验 3.进程和线程 4.多线程的概念及原理 5.多线程的优缺点和一个Tip 6.主线程 7.技术方案 二.Pthrea ...

  9. iOS多线程到底不安全在哪里?

    iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题. 共享状态,多线程共同访问某个对象的property,在iOS编程里是很普遍的使用场景,我们就从P ...

随机推荐

  1. JavaScript函数劫持

    一.为什么我会写这篇文章 这篇文章其实是在一个偶然的机会下发现了居然有JavaScript劫持这种东西,虽然这种东西在平时用的比较少,而且一般实用价值不高,但是在一些特殊的情况下还是要使用到的,所以在 ...

  2. Word Excel 操作总结

    1.与office无关使用 Aspose.Cells.dll,Aspose.Words.dll 2.使用Microsoft.Office.Interop.Excel Microsoft.Office. ...

  3. js如何判断一个数组

    typeof [] 为一个"object" 不能通过此方法判断一个数组 方法 1.instanceof方法,这个方法用的比较多. 2.这个是es5以后推荐的方法,Object.pr ...

  4. matlab 画图中线型及颜色设置

    matlab受到控制界广泛接受的一个重要原因是因为它提供了方便的绘图 功能.本章主要介绍2维图形对象的生成函数及图形控制函数的使用方 法,还将 简单地介绍一些图形的修饰与标注函数及操作和控制MATLA ...

  5. javascript数组去重的4个方法

    Array.prototype.unique1 = function(){//有局限性,1,“1”的情况会被去重,因为存入临时对象时,数组中的值被统一转换成了字符串 var obj = {},newA ...

  6. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

  7. 63-w 简明笔记

    显示关于系统用户的信息 w [options] [username] w用于显示当前登录系统的用户的名字以及他们的终端设备编号.登录时间.正在运行的命令和其他一些信息 参数 username 限定仅显 ...

  8. 14-find 查找文件

    find - search for files in a directory hierarchy 查找文件 [语法]: find [选项] [参数] [功能介绍] find命令用来在指定目录下查找文件 ...

  9. clean之后R文件消失

    首先确定你的SDK是新的. 其次接下来检查你的.xml文件,文件名不能大写. 如果xml文件太多 ,那么clean一下你的项目,这时候注意看Console的提示. Console会提示你xml文件错误 ...

  10. 搭建企业内部yum仓库(centos6+centos7+epel源)

    搭建自己的yum仓库,将自己制作好的rpm包,添加到自己的yum源中. yum仓库服务端配置如下 : 1. 创建yum仓库目录 mkdir -p /data/yum_data/cd /data/yum ...