iOS中使用子线程的完整方法
http://www.cnblogs.com/ygm900/archive/2013/06/23/3151691.html
第一步:开启子线程
//开启子线程到网络上获取数据
myFirstThread = [[NSThread alloc]initWithTarget:self selector:@selector(thread1GetData) object:nil];
[myFirstThread setName:@"第一个子线程,用于获取网络数据"];
[myFirstThread start];
第二步:子线程的方法

//获取数据
-(void)thread1GetData
{
while (!myFirstThread.isCancelled) { // 关键
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
//通过接口获取数据 (字典格式)
self.dic_base64TabelViewDataSource = [self getDataFromInterFace]; //将数据字典转换成可以直接显示的cellview nsma_CellViews 相当于终极数据源
self.nsma_CellViews = [self orderDataForTableViewWithDictinary: self.dic_base64TabelViewDataSource]; //用数据源 nsma_CellViews 更新用户界面
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES]; // [NSThread sleepForTimeInterval:0.09]; //不用亦可
[pool release];
[NSThread exit]; //关键 }
}

第三步:结束子线程

-(IBAction)btnBack:(id)sender
{
//释放内存 仅仅remove 并不会触发内存的释放 if (!(mySecondThread==nil) && !myFirstThread.isCancelled) {
[myFirstThread cancel]; //等子线程结束再跳出循环
int i=0;
while (!myFirstThread.isFinished){
NSLog(@"mySecondThread还没有结束 %i",i++);
}
} //其它操作
}

iOS中使用子线程的完整方法的更多相关文章
- vc中主线程等待子线程退出的方法
VC线程同步,在子线程中等待另一子线程结束,通过WaitForSingleObject可以实现,但是如果在主线程中等待子线程结束,这个函数是无法完成要求的,因为它会造成主线程挂起,导致程序死掉.我们可 ...
- iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...
- 2017-11-29 由runnable说起Android中的子线程和主线程
1.首先纠正一个观点,就是runnable运行在子线程中是错误的观念.runnable只是创建了一个执行任务的对象,但是它本身并不会创建一个新的子线程,Runable只是给你接口让你实现工作线程的工作 ...
- iOS中的GCD线程
一.什么是GCD 全称是Grand Central Dispatch ,纯C语言编写,提供非常多强大的函数,是目前苹果官网推荐的多线程开发方法,NSOperation 便是基于GCD的封装 ...
- iOS中的NSOperation线程
1.除NSThread之外的第二种多线程的编程方法 2.采用NSOperation(线程操作,通常用他的子类)和NSOperationQueue(线程队列)搭配来做多线程开发,采用NSOperat ...
- Java中怎样创建线程安全的方法
面试问题: 下面的方法是否线程安全?怎样让它成为线程安全的方法? class MyCounter { private static int counter = 0; public static int ...
- Android子线程创建Handler方法
如果我们想在子线程上创建Handler,通过直接new的出来是会报异常的比如: new Thread(new Runnable() { public void run() { Handler hand ...
- iOS中进程与线程(多线程、主次线程)
一.什么是线程?什么是多线程? 线程是用来执行任务的,线程彻底执行完任务A才能去执行任务B.为了同时执行两个任务,产生了多线程. 例子: 打开一个音乐软件,用户开辟一个线程A ...
- IntentService用于服务中开启子线程的自动关闭
package com.pingyijinren.test; import android.app.IntentService; import android.content.Intent; impo ...
随机推荐
- sql server 2008 System.Data.SqlClient.SqlException (0x80131904): 查询处理器未能为执行并行查询启动必要的线程资源 处理方法
修改并行度: 修改了这个“最大并行度”,如果再没出现 cxpacket应该没问题了 参考资料:http://jingyan.baidu.com/article/5d6edee22daf8799eade ...
- poj 1094 Sorting It All Out(nyoj 349)
点击打开链接 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24544 Accep ...
- cocos2dx一个场景添加多个层
首先创建两个layer,以下是头文件 #pragma once#include "cocos2d.h"USING_NS_CC;class BackgroundLayer : pub ...
- 动手学servlet(四) cookie和session
Cookie cookie是保存在客户端的一个“键值对”,用来存储用户的一些信息 cookie的应用: -在电子商务会话中标识用户 -对网站进行定制,比如你经常浏览哪些内容,就展示哪些页面给你 - ...
- MAC 下用GCC编译报错:“Undefined symbols for architecture x86_64: ”
解决方法: 因为GCC编译的时候没有链接C++ standard library, 因此在编译的时候要加入符号 -lstdc++ 使用命令: gcc myprog.c -o myprog -lstdc ...
- SICP 1.1-1.5
1.1 a = b = nil 1.2 (/ (+ (- (- (+ (/ ))))) (* (- ) (- )) 1.3 a = b = nil 1.4... 1.5 (define (p) (p) ...
- Debian的一个命令
dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包.下面是它的一些命令解释:1)安装软件命令行:dpkg -i <.deb file name> ...
- Android开发-API指南-<category>
<category> 英文原文:http://developer.android.com/guide/topics/manifest/category-element.html 采集(更新 ...
- sendmessage和postmessage的区别
BOOL PostMessage( HWND hWnd, // handle of destination window ...
- Gem5全系统模式下运行SPLASH-2 Benchmarks使用alpha ISA
Steps to run the SPLASH-2 Benchmarks on M5 in full system mode using the alpha ISA. This Guide is ai ...