20160319多线程复习
2016-04-10 本文已影响59人
訷訷
- (void)GCD
{
/**核心:队列+任务*/
#pragma mark - 队列
// 全局并发
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 主队列
dispatch_get_main_queue();
// 自创建并发队列
dispatch_queue_create("CONCURRENT", DISPATCH_QUEUE_CONCURRENT);
// 自创建串行队列
dispatch_queue_create("SERIAL", DISPATCH_QUEUE_SERIAL);
#pragma mark - 任务
// 同步函数
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
});
// 异步函数
dispatch_async(dispatch_get_global_queue(0, 0), ^{
});
/*
同步函数:必须执行完当前任务才能到下一个
异步函数:不需要
*/
#pragma mark - 队列组
// 创建队列组
dispatch_group_t group = dispatch_group_create();
// 任务
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
});
// 任务执行完调用
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
});
}
- (void)NSOperation {
/**核心:队列+操作*/
#pragma mark - NSBlockOperation
// 创建NSBlockOperation
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
// 主线程执行
}];
// 追加操作
[blockOperation addExecutionBlock:^{
// 子线程执行
}];
// 启动操作
[blockOperation start];
#pragma mark - NSOperationQueue
// 主队列
[NSOperationQueue mainQueue];
// 非主队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 添加操作到队列
[queue addOperation:blockOperation];
// 简便方法
[queue addOperationWithBlock:^{
}];
// 设置操作依赖
[blockOperation addDependency:[[NSOperation alloc] init]];
}
- (void)NSURLSession {
#pragma mark - GET请求
{
// 1.创建NSURLSession
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建task
// 创建URL
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=520it&pwd=520it&type=JSON"];
// 创建请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 根据session创建task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 子线程执行
}];
// 3.执行task
[dataTask resume];
}
#pragma mark - POST请求
{
// 1.创建NSURLSession
NSURLSession *session = [NSURLSession sharedSession];
// 2.创建task
// 创建URL
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
// 创建POST请求
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
mutableRequest.HTTPMethod = @"POST";
mutableRequest.HTTPBody = [@"username=520it&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
// 根据session创建task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 子线程执行
}];
// 3.执行task
[dataTask resume];
}
- (void)NSURLSessionDelegate {
{
// 1.创建NSURLSession
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
// 2.创建task
// 创建URL
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
// 创建POST请求
NSURLRequest *Request = [NSMutableURLRequest requestWithURL:url];
// 根据session创建task
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:Request];
// 3.执行task
[dataTask resume];
}
}
// 1.接收到服务器响应
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
// 通过completionHandler回调告诉系统如何处理服务器返回的数据
completionHandler(NSURLSessionResponseAllow);
}
// 2.接受到服务器返回的数据,可能多次调用
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
}
// 3.完成或失败时调用
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
}
- (void)AFN {
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *dict = @{
@"username":@"520it",
@"pwd":@"520",
@"type":@"JSON"
};
[manager POST:@"http://120.25.226.186:32812/login" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"http://www.baidu.com" parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
}
```