I'm trying to implement a file storage queue subsystem, as described in wwdc 2012 Asynchronous Design Patterns with Blocks, GCD, and XPC. I have a custom concurrent processing queue that formats the data and hands over the result to my custom concurrent storage queue. The storage queue then creates an dispatch\_io\_t channel for writing, splits the file (if I don't split the file I get memory issues with large data), and writes each chunk with a dispatch\_io\_write. Sometimes the write completes without errors, but often I get either `EFAULT` or `EDESTADDRREQ` for any one of the chunks

My general approach is outlined as follows:

```
dispatch_async(processingQueue, ^{

// prepare dispatch data for storageQueue

dispatch_async(storageQueue, ^{

    dispatch_io_t writeChannel = dispatch_io_create_with_path(DISPATCH_IO_STREAM,
                                                              [pathString UTF8String],
                                                              O_RDWR|O_CREAT, // read-write, create if not exist
                                                              S_IRWXU|S_IRWXG|S_IRWXO, // set all permissions
                                                              storageQueue,
                                                              ^(int error) {

                                                              });

    __block size_t chunkSize = STORAGE_WRITE_CHUNK_SIZE;

    __block off_t currentOffset = 0;

    dispatch_io_set_high_water(writeChannel, chunkSize);

    for (currentOffset = 0; currentOffset < imageDataSize; currentOffset += chunkSize) {

        // is dispatch_barrier required?
        dispatch_data_t blockData = dispatch_data_create_subrange(dictData,
                                                                  currentOffset,
                                                                  MIN(imageDataSize - currentOffset,chunkSize));

        if (dispatch_data_get_size(blockData) > 0) {

            dispatch_io_write(writeChannel,
                              currentOffset,
                              blockData,
                              storageQueue,
                              ^(bool done, dispatch_data_t data, int error){

                              });

        } else {

            NSLog(@"Error chunking data for writing!!");
            break;

        }
    }

    dispatch_io_close(writeChannel,0);
});
});

```

I have tried using serial queues, wrapping in `dispatch_barrier` blocks, modifying the `dispatch_io channel` flags, but the problem persists - frequent errors in `dispatch_io_write` due to bad address or destination

Would like to have some answers to:  

1. What is the recommended way of doing buffered file writing using `dispatch_io_write` and a custom concurrent queue?  

2. Do I need to use `dispatch_barrier`, and if so, `dispatch_io_barrier` of the channel or `dispatch_barrier_async` of the queue?  

3. Are there any channel flags that I should be aware of in such a scenario?