我无法理解为 dispatch_io_read 函数调用提供给数据应用程序的偏移量变量。我看到文档声称偏移量是与数据对象基础的逻辑偏移量。查看 dispatch_data_apply 函数的源代码可以确认,对于第一次申请数据块,该变量总是从 0 开始,然后只是范围长度的总和。
我想我不明白这个变量的目的。我最初认为这是整个读取的偏移量,但事实并非如此。看来您必须跟踪读取的字节数和该数量的偏移量才能真正正确地在 libdispatch 中进行读取。
// Outside the dispatch_io_read handler...
char * currBufferPosition = destinationBuffer;
// Inside the dispatch_io_read handler...
dispatch_io_read(channel, fileOffset, bytesRequested, queue, ^(bool done, dispatch_data_t data, int error) {
// Note: Real code would handle error variable.
dispatch_data_apply(data, ^bool(dispatch_data_t region, size_t offset, const void * buffer, size_t size) {
memcpy(currBufferPosition, buffer, size);
currBufferPosition += size;
return true;
});
});
我的问题是:这是使用 dispatch_data_apply 返回的数据的正确方法吗?如果是这样,传递给应用程序处理程序的偏移变量的目的是什么?对我来说,文档似乎并不清楚。