iOS开发中一些常见的并行处理
- NSManagedObjectContext* context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
- context.persistentStoreCoordinator = self.persistentStoreCoordinator;
- context.undoManager = nil;
- [self.context performBlockAndWait:^
- {
- [self import];
- }];
- [lines enumerateObjectsUsingBlock:
- ^(NSString* line, NSUInteger idx, BOOL* shouldStop)
- {
- NSArray* components = [line csvComponents];
- if(components.count < 5) {
- NSLog(@"couldn't parse: %@", components);
- return;
- }
- [Stop importCSVComponents:components intoContext:context];
- }];
- ImportOperation* operation = [[ImportOperation alloc]
- initWithStore:self.store fileName:fileName];
- [self.operationQueue addOperation:operation];
- if(self.isCancelled) {
- *shouldStop = YES;
- return;
- }
- operation.progressCallback = ^(float progress)
- {
- [[NSOperationQueue mainQueue] addOperationWithBlock:^
- {
- self.progressIndicator.progress = progress;
- }];
- };
- self.progressCallback(idx / (float) count);
- NSInteger progressGranularity = lines.count / 100;
- if (idx % progressGranularity == 0) {
- self.progressCallback(idx / (float) count);
- }
- [[NSNotificationCenter defaultCenter]
- addObserverForName:NSManagedObjectContextDidSaveNotification
- object:nil
- queue:nil
- usingBlock:^(NSNotification* note)
- {
- NSManagedObjectContext *moc = self.mainManagedObjectContext;
- if (note.object != moc)
- [moc performBlock:^(){
- [moc mergeChangesFromContextDidSaveNotification:note];
- }];
- }];
- }];
- __weak id weakSelf = self;
- [self.operationQueue addOperationWithBlock:^{
- NSNumber* result = findLargestMersennePrime();
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- MyClass* strongSelf = weakSelf;
- strongSelf.textLabel.text = [result stringValue];
- }];
- }];
- UIGraphicsBeginImageContextWithOptions(size, NO, 0);
- // drawing code here
- UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return i;
- // Warning: please don't use this code.
- dispatch_async(backgroundQueue, ^{
- NSData* contents = [NSData dataWithContentsOfURL:url]
- dispatch_async(dispatch_get_main_queue(), ^{
- // do something with the data.
- });
- });
- - (void)start
- {
- NSURLRequest* request = [NSURLRequest requestWithURL:self.url];
- self.isExecuting = YES;
- self.isFinished = NO;
- [[NSOperationQueue mainQueue] addOperationWithBlock:^
- {
- self.connection = [NSURLConnectionconnectionWithRequest:request
- delegate:self];
- }];
- }
- - (void)cancel
- {
- [super cancel];
- [self.connection cancel];
- self.isFinished = YES;
- self.isExecuting = NO;
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- self.data = self.buffer;
- self.buffer = nil;
- self.isExecuting = NO;
- self.isFinished = YES;
- }
- Concurrency Programming Guide
- NSOperation Class Reference: Concurrent vs. Non-Concurrent Operations
- Blog: synchronous vs. asynchronous NSURLConnection
- GitHub: SDWebImageDownloaderOperation.m
- Blog: Progressive image download with ImageIO
- WWDC 2012 Session 211: Building Concurrent User Interfaces on iOS
- @interface Reader : NSObject
- - (void)enumerateLines:(void (^)(NSString*))block
- completion:(void (^)())completion;
- - (id)initWithFileAtPath:(NSString*)path;
- @end
- - (void)enumerateLines:(void (^)(NSString*))block
- completion:(void (^)())completion
- {
- if (self.queue == nil) {
- self.queue = [[NSOperationQueue alloc] init];
- self.queue.maxConcurrentOperationCount = 1;
- }
- self.callback = block;
- self.completion = completion;
- self.inputStream = [NSInputStream inputStreamWithURL:self.fileURL];
- self.inputStream.delegate = self;
- [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
- forMode:NSDefaultRunLoopMode];
- [self.inputStream open];
- }
- - (void)stream:(NSStream*)stream handleEvent:(NSStreamEvent)eventCode
- {
- switch (eventCode) {
- ...
- case NSStreamEventHasBytesAvailable: {
- NSMutableData *buffer = [NSMutableData dataWithLength:4 * 1024];
- NSUInteger length = [self.inputStream read:[buffer mutableBytes]
- maxLength:[buffer length]];
- if (0 < length) {
- [buffer setLength:length];
- __weak id weakSelf = self;
- [self.queue addOperationWithBlock:^{
- [weakSelf processDataChunk:buffer];
- }];
- }
- break;
- }
- ...
- }
- }
- - (void)processDataChunk:(NSMutableData *)buffer;
- {
- if (self.remainder != nil) {
- [self.remainder appendData:buffer];
- } else {
- self.remainder = buffer;
- }
- [self.remainder obj_enumerateComponentsSeparatedBy:self.delimiter
- usingBlock:^(NSData* component, BOOL last) {
- if (!last) {
- [self emitLineWithData:component];
- } else if (0 < [component length]) {
- self.remainder = [component mutableCopy];
- } else {
- self.remainder = nil;
- }
- }];
- }
iOS开发中一些常见的并行处理的更多相关文章
- iOS开发中一些常见的并行处理(转)
本文主要探讨一些常用多任务的最佳实践.包括Core Data的多线程访问,UI的并行绘制,异步网络请求以及一些在运行态内存吃紧的情况下处理大文件的方案等. 其实编写异步处理的程序有很多坑!所以,本文 ...
- iOS开发中常见的一些异常
iOS开发中常见的异常包括以下几种NSInvalidArgumentExceptionNSRangeExceptionNSGenericExceptionNSInternallnconsistency ...
- iOS 开发中常见的设计模式
最近有小伙伴问到在iOS开发中的几种设计模式,这里摘录一下别人的总结(因为已经感觉总结得差不多了,适用的可以阅读一下) 首先是开发中的23中设计模式分为三大类:1.创建型 2.结构型 3.行为型 (i ...
- 总结iOS开发中的断点续传那些事儿
前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...
- iOS开发中静态库制作 之.a静态库制作及使用篇
iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...
- iOS开发中常见问题集锦
在iOS开发中,会出现各种各样的问题.今天,就把这些常见的问题以及各位大牛的解决方案汇总下,方便以后查阅: 常见错误: 1. linker command failed with exit code ...
- iOS开发UI篇—常见的项目文件介绍
iOS开发UI篇—常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要 ...
- 简述 Ruby 与 DSL 在 iOS 开发中的运用
阅读本文不需要预先掌握 Ruby 与 DSL 相关的知识 何为 DSL DSL(Domain Specific Language) 翻译成中文就是:"领域特定语言".首先,从定义就 ...
- 深入理解 iOS 开发中的锁
来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89474/ 点击 → 申请加入伯乐在线专栏作者 摘要 本文的目的不是介绍 iOS 中各种锁如何使用,一方面笔者没有大 ...
随机推荐
- input text 的事件及方法
事件 描述onactivate 当对象设置为活动元素时触发.onafterupdate 当成功更新数据源对象中的关联对象后在数据绑定对象上触发.onbeforeactivate 对象要被设置为当前元素 ...
- 用dom操作替代正则表达式
在B/S结构客户端越来越“胖”的今天,作为一名全端程序员,您很可能会在前端操作html字符串,注意,是操作html字符串,不是操作当前页面的html. 举个例子,百度推出的在线HTML富文本编辑器Ue ...
- AngularJS快速入门指南09:SQL
我们可以将之前章节中的代码用来从数据库中读取数据. 通过PHP Server从MySQL数据库中获取数据 <div ng-app="myApp" ng-controller= ...
- WeUI 为微信 Web 服务量身设计-h5前端框架
WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell.dialog. progress. toas ...
- crossplatform---bower解决js的依赖管理
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
- paip.mysql 全文索引查询空白解决
paip.mysql 全文索引查询空白解决 或者 Incorrect key file for table: \'%s\'. Try to repair it 作者Attilax 艾龙, ...
- iOS-SVN、Git
版本控制SVN和Git使用详解 公司的实际开发中,在天朝使用较多的还是SVN,因为SVN是集中式的,在天朝上班你们都懂的! -----------------svn--------- ...
- javaweb学习总结(二十一)——JavaWeb的两种开发模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. 一.JSP+JavaBean开发模式 1 ...
- node.js WebService异常处理(domain)以及利用domain实现request生命周期的全局变量
成熟的Web Service技术,例如Fast CGI.J2EE.php,必然会对代码异常有足够的保护,好的Web必然会在出错后给出友好的提示,而不是莫名其妙的等待504超时.而node.js这里比较 ...
- 深入理解.NET程序的原理 谈一谈破解.NET软件的工具和方法
最近一段时间不忙,闲下来的空闲时间,重读了一下CLR的原理,回味一下有关程序集的的知识,顺便练了一下手,学习致用,破解了若干个.NET平台的软件.以此来反观.NET程序开发中,需要注意的一些问题. 基 ...