object-c中创建线程的方法有哪些?在主线程中执行代码用什么方法?如果想要延时执行代码,用什么方法?
2014-05-12 14:19
465 查看
1.object-c中用NSThread创建线程的方法:
也可以是
2.object-c中用NSOperation创建线程的方法
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:3];
NSString *str=@"nice to meet you";
;
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
UILabel *onelabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:onelabel];
onelabel.text=str;
}];
}];
3.object-c中用GCD创建线程的方法
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:3];
NSString *str=@"nice to meet you";
NSLog(@"%@",str);
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *onelabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:onelabel];
onelabel.text=str;
});
});
can assume for example, that a
block 代码端这个还不是很懂
__block NSData *dataFromServer = nil;
NSBlockOperation *downloadOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakDownloadOperation = downloadOperation;
[weakDownloadOperation addExecutionBlock:^{
// Download your stuff
// Finally put it on the right place:
dataFromServer = ....
}];
NSBlockOperation *saveToDataBaseOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakSaveToDataBaseOperation = saveToDataBaseOperation;
[weakSaveToDataBaseOperation addExecutionBlock:^{
// Work with your NSData instance
// Save your stuff
}];
[saveToDataBaseOperation addDependency:downloadOperation];
[myQueue addOperation:saveToDataBaseOperation];
[myQueue addOperation:downloadOperation];
在主线程中执行代码用什么方法?
NSThread *oneThread=[[NSThread alloc]initWithTarget:self selector:@selector(sayMethod) object:nil]; [oneThread start];还可以是
[self performSelectorInBackground:@selector(sayMethod) withObject:nil];
也可以是
[NSThread detachNewThreadSelector:@selector(sayMethod) toTarget:self withObject:nil];
2.object-c中用NSOperation创建线程的方法
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[queue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:3];
NSString *str=@"nice to meet you";
;
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
UILabel *onelabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:onelabel];
onelabel.text=str;
}];
}];
3.object-c中用GCD创建线程的方法
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[NSThread sleepForTimeInterval:3];
NSString *str=@"nice to meet you";
NSLog(@"%@",str);
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *onelabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:onelabel];
onelabel.text=str;
});
});
NSOperationQueuegives finer control with what you want to do. You can create dependencies between the two operations (download and save to database). To pass the data between one block and the other, you
can assume for example, that a
NSDatawill be coming from the server so:
block 代码端这个还不是很懂
__block NSData *dataFromServer = nil;
NSBlockOperation *downloadOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakDownloadOperation = downloadOperation;
[weakDownloadOperation addExecutionBlock:^{
// Download your stuff
// Finally put it on the right place:
dataFromServer = ....
}];
NSBlockOperation *saveToDataBaseOperation = [[NSBlockOperation alloc] init];
__weak NSBlockOperation *weakSaveToDataBaseOperation = saveToDataBaseOperation;
[weakSaveToDataBaseOperation addExecutionBlock:^{
// Work with your NSData instance
// Save your stuff
}];
[saveToDataBaseOperation addDependency:downloadOperation];
[myQueue addOperation:saveToDataBaseOperation];
[myQueue addOperation:downloadOperation];
在主线程中执行代码用什么方法?
相关文章推荐
- OC中创建线程的方法是什么?如果在主线程中执行,方法是什么?如果想延时执行代码,方法是什么?
- 如果重写了service方法应该添加什么代码来保证你的doGet或doPost方法能正常执行
- object-c中 延时执行代码的方法
- 执行数据库查询时,如果要查询的数据有很多,假设有1000万条,用什么办法可以提高查询速率?在数据库方面或java代码方面有什么优化的方法
- EF异常‘在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。 请注意不保证 DbContext 的实例成员’
- 线程在什么情况下会死锁?预防死锁的方法有哪些?
- Thread类的sleep()方法和对象的wait()方法都可以让线程暂停执行,它们有什么区别?
- Android平台在UI Thread中执行非UI线程中定义的代码片段的几种方法
- Handler的post方法创建的线程和UI线程有什么关系?
- 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不
- Java实现等待所有子线程结束后再执行一段代码的方法
- Thread类的sleep()方法和对象的wait()方法都能使线程暂停执行,他们有什么区别?
- ExtTag,如果你不想写extjs那么麻烦的代码可以尝试使用html方式的方法来创建extjs控件.
- iOS创建线程方式(以及回到主线程, 以及延时之行代码)
- SQL Server 第四堂课,创建存储过程。存储过程是一组编译在单个执行计划中的transact-SQL语句。存储过程相当于C#函数,可以允许模块化程序设计,允许更快执行如果某操作需要大量transct-SQL代码或需要重复执行,将在创建存储过程中对其进行分析和优化。
- 使用代码创建的控件对象,设置的事件监听方法不执行的问题
- Java创建并执行线程的四种方法
- JavaScript的执行原理,很多人都理解错了:在js被加载后,其实就将js代码执行了一遍,在内存中创建了所有js文件中的变量。而不是激发了某个js方法后,再去相应的js文件中去执行,是去内存中执行
- oracle中创建java包.执行JAVA代码的方法
- Qt程序主线程执行大量计算 界面卡顿,有什么方法优化?