NSOperation的继承实现
2015-05-09 19:30
239 查看
/*
1.线程同步:原子操作,条件锁,递归锁,@synchronized。NSCondition:同步执行的顺序。
2.线程间通信:
在主线程做事:-performSelectorOnMainThread
在子线程做事:-performSelectorInBackground
在指定线程做事:-performSelector: onThread: withObject: waitUntilDone:
在当前线程做事:-performSelector: withObject:
取消发送给当前线程的某消息:+cancelPreviousPerformRequestsWithTarget: selector: object:
*/
对NSOperation继承实现,进行队列的操作。
官方实现:
NSBlockOperation
NSInvocationOperation
自定义实现:
.h
#import <Foundation/Foundation.h>
typedef void (^CompletedBlock) (NSDictionary *dictionary);
@interface MyOperation : NSOperation
@property (nonatomic, strong) NSURL *imageUrl;
@property (nonatomic, strong) id target;
@property (nonatomic, strong) CompletedBlock completedBlock;
- (instancetype)initWithUrl:(NSURL*)imageUrl target:(id)target completedBlock:(CompletedBlock)completedBlock;
@end.m
#import "MyOperation.h"
@implementation MyOperation
- (void)main {
NSData *data = [NSData dataWithContentsOfURL:self.imageUrl];
NSDictionary *dict = @{@"data":data};
self.completedBlock(dict);
}
- (instancetype)init{
return nil;
}
- (instancetype)initWithUrl:(NSURL*)imageUrl target:(id)target completedBlock:(CompletedBlock)completedBlock{
self = [super init];
if (self) {
_imageUrl = imageUrl;
_target = target;
_completedBlock = completedBlock;
}
return self;
}
@end应用举例:
NSString *urlStr = @"http://b.hiphotos.baidu.com/image/pic/item/9e3df8dcd100baa1e477e5524310b912c9fc2efb.jpg";
MyOperation *opera = [[MyOperation alloc]initWithUrl:[NSURL URLWithString:urlStr] target:self completedBlock:^(NSDictionary *dictionary) {
UIImage *image = [UIImage imageWithData:dictionary[@"data"]];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 290, 80, 120)];
imageView.image = image;
[self.view addSubview:imageView];//回调根据你加的queue,会在不同的线程执行。
NSThread *cu = [NSThread currentThread];
cu.name = @"current";
NSString *str = [NSThread mainThread].name;
NSLog(@"%@= %@", str, cu);
}];
// NSOperationQueue *myq = [[NSOperationQueue alloc]init];
// [myq addOperation:opera];//子线程执行completedBlock
[[NSOperationQueue mainQueue] addOperation:opera];//主线程执行completedBlock
1.线程同步:原子操作,条件锁,递归锁,@synchronized。NSCondition:同步执行的顺序。
2.线程间通信:
在主线程做事:-performSelectorOnMainThread
在子线程做事:-performSelectorInBackground
在指定线程做事:-performSelector: onThread: withObject: waitUntilDone:
在当前线程做事:-performSelector: withObject:
取消发送给当前线程的某消息:+cancelPreviousPerformRequestsWithTarget: selector: object:
*/
对NSOperation继承实现,进行队列的操作。
官方实现:
NSBlockOperation
NSInvocationOperation
自定义实现:
.h
#import <Foundation/Foundation.h>
typedef void (^CompletedBlock) (NSDictionary *dictionary);
@interface MyOperation : NSOperation
@property (nonatomic, strong) NSURL *imageUrl;
@property (nonatomic, strong) id target;
@property (nonatomic, strong) CompletedBlock completedBlock;
- (instancetype)initWithUrl:(NSURL*)imageUrl target:(id)target completedBlock:(CompletedBlock)completedBlock;
@end.m
#import "MyOperation.h"
@implementation MyOperation
- (void)main {
NSData *data = [NSData dataWithContentsOfURL:self.imageUrl];
NSDictionary *dict = @{@"data":data};
self.completedBlock(dict);
}
- (instancetype)init{
return nil;
}
- (instancetype)initWithUrl:(NSURL*)imageUrl target:(id)target completedBlock:(CompletedBlock)completedBlock{
self = [super init];
if (self) {
_imageUrl = imageUrl;
_target = target;
_completedBlock = completedBlock;
}
return self;
}
@end应用举例:
NSString *urlStr = @"http://b.hiphotos.baidu.com/image/pic/item/9e3df8dcd100baa1e477e5524310b912c9fc2efb.jpg";
MyOperation *opera = [[MyOperation alloc]initWithUrl:[NSURL URLWithString:urlStr] target:self completedBlock:^(NSDictionary *dictionary) {
UIImage *image = [UIImage imageWithData:dictionary[@"data"]];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 290, 80, 120)];
imageView.image = image;
[self.view addSubview:imageView];//回调根据你加的queue,会在不同的线程执行。
NSThread *cu = [NSThread currentThread];
cu.name = @"current";
NSString *str = [NSThread mainThread].name;
NSLog(@"%@= %@", str, cu);
}];
// NSOperationQueue *myq = [[NSOperationQueue alloc]init];
// [myq addOperation:opera];//子线程执行completedBlock
[[NSOperationQueue mainQueue] addOperation:opera];//主线程执行completedBlock
相关文章推荐
- JavaScript是如何实现继承的(六种方式)
- 从内存布局看C++虚继承的实现原理
- Javascript--原型链实现继承
- 通过 Hibernate 实现 JPA 对象关系模型之继承映射策略
- 区分接口继承和实现继承
- java实现多继承
- C# 中关于接口实现、显示实现接口以及继承
- effective C++ 学习笔记 实现&&继承与面向对象设计
- java中的接口为什么可以多继承,而其他类则不能实现多继承
- Lua类和类继承实现
- 继承Application实现Android数据共享
- c# 关于继承类中构造函数的实现
- JAVA笔记 Runnable 和 Thread 实现方式和继承方式的区别
- 泛型的继承与实现
- 在VB6下如何实现继承,重载和多态----DynamicClassFactory
- 通过继承SecurityManager实现系统退出检查
- 我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承
- java 接口 实现和继承关系
- javascript 004 基于原型链实现类的继承
- js中的继承实现