iOS 多线程运用

  • A1_608880
    了解作者
  • 384.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-04 02:03
    上传日期
里面有3个demo,各种多线程详解,以及多线程使用,为了帮助更多的人理解多线程
多线程运用.zip
内容介绍
# GCD_Request GCD的使用:多个网络请求/任务并发或顺序执行,所有的网络请求/任务都结束之后再执行数据操作。 ###更多内容请移步至简书地址:[Charles姚](http://www.jianshu.com/p/e05f2afc2026) ![GCD12345.gif](http://upload-images.jianshu.io/upload_images/939127-ca8e8ad5c74fdb1c.gif?imageMogr2/auto-orient/strip) ####部分代码: ``` static NSString *const kCellID = @"kCellID"; static NSString *const kDemo1Text = @"多个网络请求/任务并发执行,所有的网络请求/任务都结束之后再执行数据操作,点击查看Demo/实例"; static NSString *const kDemo2Text = @"多个网络请求/任务顺序执行,所有的网络请求/任务都结束之后再执行数据操作,点击查看Demo/实例"; ``` ``` 多个网络请求/任务并发结束之后,然后再执行操作。  // 1.     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[0]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);     });          // 2.     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[1]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);     });          // 3.     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[2]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);     });          // 4.     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[3]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);     });          dispatch_group_notify(_group, dispatch_get_main_queue(), ^{         [self.tableView reloadData];     }); ``` ``` 多个网络请求/任务顺序执行结束之后,然后再执行操作。     dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);          // 1.     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // -1     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[0]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);         dispatch_semaphore_signal(semaphore); // + 1     });          // 2.     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[1]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);         dispatch_semaphore_signal(semaphore);     });          // 3.     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[2]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);         dispatch_semaphore_signal(semaphore);     });          // 4.     dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);     dispatch_group_enter(_group);     dispatch_group_async(_group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.urlStrs[3]]];         CYGCDImageModel *model = [CYGCDImageModel new];         model.imageData = data;         [self.dataArray addObject:model];         dispatch_group_leave(_group);         dispatch_semaphore_signal(semaphore);     });          dispatch_group_notify(_group, dispatch_get_main_queue(), ^{         [self.tableView reloadData];     }); ```
评论
    相关推荐
    • ios
      适用于iOS的Currency Converter应用程序
    • ReplayKit iOS
      iOS视频录制 绝对好东西
    • ios HotelApp
      ios 完整的HotelApp 欢迎一起学习和探讨
    • iOS开发
      iOS开发
    • iOS 播放器
      iOS 播放器
    • ios Drawing
      iOS’s drawing routines are primarily sourced from the UIKit and QuartzCore frameworks.
    • ios Apprentice
      ios app 书籍 ios Apprentice v5.0 一到四章节pdf code
    • iOS内存
      iOS内存 自定义按钮 UIcolorConvertor->将十六进制转换为UIColor
    • ios
      ios
    • IOS programming
      最新版的 ios12 programming;这个是我 最近找的;希望对大家有用