Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:56:10+00:00 2026-05-31T05:56:10+00:00

With Grand Central Dispatch, you can schedule reads and write without needing to worry

  • 0

With Grand Central Dispatch, you can schedule reads and write without needing to worry much about when/how this happens. Compared with my previous NSStream based approach, this requires fewer management on the outside. However, my naive implementation is slower than my NSStream based approach.

For NSStream, I queried the preferred IO size of both the source and destination (NSURLPreferredIOBlockSizeKey). Then I read in whole “preferred input sized chunks” into a buffer, and as soon as I had at least “preferred output size” bytes in the buffer I wrote whole chunks to the destination (except for the last chunk, of course). This should be pretty close to the optimum with respect to read and write performance.

However, with GCD, I don’t have much influence on this. Imagine that the source has a preferred IO size of 100kB and that the target has a preferred IO size for the target is 1MB: my naive implementation would now write 10 times as often as it would in my NSStream based solution.

So, what’s the most efficient way to solve this with GCD? Simply write to a buffer in the reader block, and as soon as enough data has been gathered, schedule a write block of “preferred output size”? I imagine GCD might offer me a solution here that I’m not aware of yet.

Here’s the most important part of my current GCD solution:

// input_ and output_ are of type dispatch_io_t

dispatch_io_read(
    input_,
    0,
    SIZE_MAX,
    dispatch_get_main_queue(),
    ^(bool done, dispatch_data_t data, int error) {
        size_t data_size;

        if (error) {
            NSLog(@"Input: error %d", error);
            [self cancel];
            return;
        }
        if (data) {
            data_size = dispatch_data_get_size(data);
            if (data_size > 0) {
                dispatch_io_write(
                    output_,
                    0,
                    data,
                    dispatch_get_main_queue(), ^(bool done, dispatch_data_t data, int error) {
                        // TODO: I don't know how to get the offset (for progress). So I need to
                        // pass it from the calling block.
                        if (error) {
                            NSLog(@"Output: error %d", error);
                            return;
                        }
                        if (done) {
                            bytesWritten_ += data_size;
                            // Update progress report here.
                        }
                    }
                );
            }
        }
    }
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:56:12+00:00Added an answer on May 31, 2026 at 5:56 am

    Although it should not be necessary in most situations, you can influence the IO size used by GCD with the dispatch_io_set_high_water(3) and dispatch_io_set_low_water(3) APIs.

    GCD will not read or write chunks larger than a channel’s high water mark. Read/write handlers will also never be called with a data object that is smaller than the low water mark.

    E.g. by setting the low water mark of input_ in your example to 1MB you can ensure that your current read callback does not pass data objects smaller than 1MB to dispatch_io_write(3).

    If this control doesn’t suffice in your situation, you could also combine multiple data objects received from successive invocations of your read handler via dispatch_data_create_concat(3) until they reach a size big enough to pass to dispatch_io_write(3).

    Hopefully this should not be necessary however, setting the source side’s low water mark to a multiple of the preferred source chunk size big enough to reach the preferred destination chunk size and setting the destination channel’s high water mark to the preferred destination chunk size (or a multiple thereof) should give you the same performance as your current NSStream-based solution.

    You can check out the specifics of the GCD IO buffer policy in the implementation.

    In any case, please make sure to file a bug with the specifics of any case where you see performance issues with the default GCD IO buffering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm confused about how to correctly dispose of a Grand Central Dispatch I/O channel
From what I've read about Grand Central Dispatch, GCD does not do preemptive multitasking;
This is related to the Grand Central Dispatch API used in objective-c, with the
Apple introduced Grand Central Dispatch (a thread pool) in Snow Leopard, but haven't gone
Anybody have any thoughts on Grand Central Dispatch (which has now been open-sourced by
How do I get blocks/grand central dispatch working in Snow Leopard? It appears that
I use Grand Central Dispatch methods to do some executions of my app in
One of the patterns presented at the WWDC 2010 Blocks and Grand Central Dispatch
Does the Grand Central Dispatch API allow an execution context (thread) to query any
Apple's Grand Central Dispatch (GCD) is great, but only works on iOS 4.0 or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.