多线程的实现: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. node基础06:回调函数

    1.Node异步编程 Node.js 异步编程的直接体现就是回调. 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了. 回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,No ...

  2. ReactNative真机运行指南

    ReactNative真机运行指南 注意在iOS设备上运行React Native应用需要一个Apple Developer account并且把你的设备注册为测试设备.本向导只包含React Nat ...

  3. 通过js动态生成页面表格

    var redlineTemplateP = $(".redlineDataList"); for (var index in detailArraryLists.rows){ v ...

  4. JavaScript:立即执行的函数表达式

    先要理解清楚几个概念:   以下转自:http://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html  问题的核心 当你声明类似functio ...

  5. 在windows下安装配置Ulipad

    在windows下安装配置Ulipad 今天推荐一款轻便的文本编辑器Ulipad,用来写一些小的Python脚本非常方便. Ulipad下载地址: https://github.com/limodou ...

  6. Oracle 常用操作【01】修改、更新数据

    1. oracle 修改表名.列名.字段类型.添加表列.删除表列  alert table scott.test rename to test1--修改表名 alter table scott.tes ...

  7. C# 调用一个按钮的Click事件(利用反射)

    最基本的调用方法 (1)button1.PerformClick();(2)button1_Click(null,null);(3)button_Click(null,new EventArgs()) ...

  8. 取消Git代理设置

    昨天由于在用sourceTree上传下拉代码的时候,速度实在太慢,就照着百度上的方法设置了代理,结果导致sourceTree无法访问服务器,经检查排除发现可能是因为公司网络不能使用代理,被防火墙挡住了 ...

  9. Beta项目冲刺 --第二天

    在几kb的上传速度中苦苦挣扎的程序员... 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照 ...

  10. [转]hibernate在eclipse的逆向工程生成hbm.xml和bean类

    原文地址:http://www.xuebuyuan.com/210489.html 以前一直用myelipse,在myeclipse做hibernate逆向工程倒是很顺手了. 可是最近改用eclips ...