1.Dispatch I/O 简单介绍
- 在《iOS和OSX多线程和内存管理》上看到Dispatch I/O的介绍,介绍了一个简单应用就是日至API中使用到了这个技术
- 日至输出源码
- 想在网上找一些应用的介绍和详细使用,很可惜没找到什么有用的,很多都是把书中内容拷贝了一遍,实际使用和一些参数的使用并没有涉及
- 文章只是涉及了文件本地读取,文件的远程读取&写入,会在后续的文章中更新
- 应用1 : 串行异步读取本地文件
- 应用2 : 并发异步读取本地文件
- 我的BLOG更新,如果有什么错误请指导和指正
- 上一段官方注释和解析,可能有点难理解,可以根据具体应用去分析(也因为我的英语水平太烂了)
/*! @header
* Dispatch I/O provides both stream and random access asynchronous read and write operations on file descriptors.
* //同时提供了stream & random 两种异步文件读写操作
* One or more dispatch I/O channels may be created from a file descriptor as either the DISPATCH_IO_STREAM type or DISPATCH_IO_RANDOM type.
* // 可同时创建 DISPATCH_IO_STREAM & DISPATCH_IO_RANDOM 单个或者多个channel
* Once a channel has been created the application may schedule asynchronous read and write operations.The application may set policies on the dispatch I/O channel to indicate the desired frequency of I/O handlers for long-running operations.
* // 一旦channel创建,应用会根据配置项去异步调度读写操作,APP会根据设置值去多线程文件操作来实现一个较长持续时间的I/O操作
*
* Dispatch I/O also provides a memory management model for I/O buffers that avoids unnecessary copying of data when pipelined between channels.
* //Dispatch I/O 也提供一个内存管理模型针对I/0 buffer ,以防止不必要的channel之间的拷贝数据
* Dispatch I/O monitors the overall memory pressure and I/O access patterns for the application to optimize resource utilization.
* //Dispatch I/O 监控APP 的总内存 和I/O访问模式,以优化资源利用率。
*/
2. 串行异步读取本地文件
- 使用场景 : 适合文章类的读取操作(PDF,md,word.txt...),因为串行队列,所以读取的文章是顺序的,在实际使用更多
- 示例读取文件大小 : 1113294 字节
2.1 代码
//为了验证设置单次读取大小,读取次数是否正确
static int serialNumberl = 0;
//异步串行读取文件
- (void)asyncSerialReadData {
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"md"];
// 见2.2 解析1
//dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 见2.2 解析2
dispatch_io_t chanel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, [path UTF8String], 0, 0, queue, ^(int error) {
});
// 见2.3 解析3
size_t water = 1024;
dispatch_io_set_low_water(chanel, water);
dispatch_io_set_high_water(chanel, water);
NSMutableData *totalData = [[NSMutableData alloc] init];
//读取操作
dispatch_io_read(chanel, 0, SIZE_MAX, queue, ^(bool done, dispatch_data_t _Nullable data, int error) {
if (error == 0) {
// 单次读取大小
size_t size = dispatch_data_get_size(data);
if (size > 0 ) {
[totalData appendData:(NSData*)data]; //拼接数据
}
}
if (done) {
// 完整读写结果
NSString *str = [[NSString alloc] initWithData:totalData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
}else {
serialNumberl ++;
NSLog(@"read not done,第%d次读取,在%@线程",serialNumberl,[NSThread currentThread]);
}
});
}
2.2 参数解析
-
解析1 : 创建队列
- 很多文章中都使用了 QOS_CLASS_DEFAULT 这个配置项,我在文中使用默认创建队列优先级参数,我觉得本地读取使用能够需求,但是也解释一下 QOS_CLASS_DEFAULT 这个配置项
- 个人觉得设计网络资源的读取和写入需要使用QOS_CLASS_DEFAULT,本地读取没有必要
//QOS: QOS(服务质量),解决网络延迟,丢包,阻塞等问题,个人觉得 /* * @constant QOS_CLASS_DEFAULT * QOS class information is not available. //涉及到使用QOS使用这个参数无效!!! * @discussion Such work is requested to run at a priority below critical user-interactive and user-initiated work, but relatively higher than utility and
background tasks. Threads created by pthread_create() without an attribute specifying a QOS class will default to QOS_CLASS_DEFAULT. This QOS class value is not intended to be used as a work classification, it should only be set when propagating or restoring QOS class values provided by the system.
//优先级低于 user-interactive配置项,这个QOS类value不涉及具体的工作内容,它应该只是传播或恢复系统提供的QOS类时默认设置。
*/
```
- 解析2 : 根据路径创建I/O Channel
- 本地读取使用这个API,可以根据path读取
/*!
* dispatch_io_create_with_path
*
* 根据path创建channel
*
* param type The desired type of I/O channel (DISPATCH_IO_STREAM or DISPATCH_IO_RANDOM) //DISPATCH_IO_STREAM顺序连续(serially)读取, DISPATCH_IO_RANDOM随机读取
* param path The absolute path to associate with the I/O channel. // 读取路径
* param oflag The flags to pass to open(2) when opening the file at path.
* param mode
* param queue
* param cleanup_handler //回调
*/
- 解析3 : 设置单次读取大小最大值,最小值
- 文件总大小1113294,设置单次读取大小为1024后,总共打印读取了1088次
- 根据具体需求来设置单次读取大小,但是有什么规则,我还没摸索出来,有知道建议的请指导
- 如果I/O处理操作需要固定大小的中间结果(既单次读取或者写入是固定大小),而不是单次随机的,则将low & high 值设置为相同的大小
- 如果不需要局部读写结果,则将low mask 设置为SIZE_MAX
```
dispatch_io_set_low_water // 单次读取mark的字节大小
* If an I/O handler requires intermediate results of fixed size, set both the low and and the high water mark to that size //如果I/O处理程序需要固定大小的中间结果,而不是单次随机的,则将low & high 值设置为相同的 *
- The default value for the low water mark is unspecified, but must be assumed to be such that intermediate handler invocations may occur.
- If I/O handler invocations with partial results are not desired, set the low water mark to SIZE_MAX. //如果不需要具有部分结果的,则将low mask 设置为SIZE_MAX。
3. 并发异步读取本地文件
- 并发读取有个实际问题:如果是文章类的,由于并发读取,所以拼接后顺序会错乱,若想要正确顺序,还需要读取时有加锁或者别的操作
- 并发读取速度远大于串行读取速度
- 代码:需要重新雕琢修改一下
3.1 代码
static int concurrentNumber = 0;
- (void)asyncConcurrentReadData {
NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"md"];
dispatch_queue_t queue = dispatch_queue_create("readDataQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
//见3.2 解析1
dispatch_fd_t fd = open(path.UTF8String, O_RDONLY);
dispatch_io_t io = dispatch_io_create(DISPATCH_IO_RANDOM, fd, queue, ^(int error) {
close(fd);
});
long long fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize;
size_t offset = 128 * 128;
NSMutableData *totalData = [[NSMutableData alloc] initWithLength:fileSize];
for (off_t currentSize = 0; currentSize <= fileSize; currentSize += offset) {
dispatch_group_enter(group);
//dispatch_io_read默认异步
dispatch_io_read(io, currentSize, offset, queue, ^(bool done, dispatch_data_t _Nullable data, int error) {
if (error == 0) {
size_t size = dispatch_data_get_size(data);
if (size > 0) {
dispatch_semaphore_wait(weakSelf.semaphore, DISPATCH_TIME_FOREVER);
[totalData appendData:(NSData*)data];
dispatch_semaphore_signal(weakSelf.semaphore);
}
}
if (done) {
dispatch_group_leave(group);
}else {
NSLog(@"read not done,第%d次读取,在%@线程",concurrentNumberl,[NSThread currentThread]);
}
});
}
dispatch_group_notify(group, queue, ^{
NSString *str = [[NSString alloc] initWithData:totalData encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
});
}
3.2 解析
-
解析1 : dispatch_fd _t,文件描述类型
typedef int dispatch_fd_t; * Native file descriptor type for the platform.//平台原生文件描述类型有不同模式 #define O_RDONLY 0x0000 // 只读 , open for reading only #define O_WRONLY 0x0001 // 只写, open for writing only #define O_RDWR 0x0002 //读写, open for reading and writing #define O_ACCMODE 0x0003 //all mode
网友评论