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 8603627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:22:07+00:00 2026-06-12T02:22:07+00:00

folks! I’m implementing a shared cache in my app. The idea is to get

  • 0

folks! I’m implementing a shared cache in my app. The idea is to get the cached data from the web in the background and then update the cache and the UI with the newly retrieved data. The trick is of course to ensure thread-safety, since the main thread will be continuously using the cache. I don’t want to modify the cache in any fashion while someone else might be using it.

It’s my understanding that using @synchronized to lock access to a shared resource is not the most elegant approach in ObjectiveC due to it trapping to the kernel and thus being rather sluggish. I keep reading that using GCD instead is a great alternative (let’s ignore its cousin NSOperation for now), and I’d like to figure out what a good pattern for my situation would be. Here’s some sample code:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

// download the data in a background thread
dispatch_async(queue, ^{
    CacheData *data = [Downloader getLatestData];

    // use the downloaded data in the main thread
    dispatch_sync(dispatch_get_main_queue(), ^{
        [AppCache updateCache:data];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CacheUpdated" object:nil];
    });
});
  1. Would this actually do what I think it does, and if so, is this the cleanest approach as of today of handling this kind of situation? There’s a blog post that’s quite close to what I’m talking about, but I wanted to double-check with you as well.
  2. I’m thinking that as long as I only ever access shared the shared resource on the same thread/queue (main in my case) and only ever update UI on main, then I will effectively achieve thread-safety. Is that correct?

Thanks!

  • 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-06-12T02:22:08+00:00Added an answer on June 12, 2026 at 2:22 am

    Yes.
    Other considerations aside, instead of shunting read/write work onto the main thread consider using a private dispatch queue.

    dispatch_queue_t readwritequeue;
    readwritequeue = dispatch_queue_create("com.myApp.cacheAccessQueue", NULL);
    

    Then update your AppCache class:

    - (void)updateCache:(id)data {
     dispatch_sync(readwritequeue, ^{ ... code to set data ... });
    }
    
    - (id)fetchData:... {
     __block id data = nil;
     dispatch_sync(readwritequeue, ^{ data = ... code to fetch data ...});
     return data;
    }
    

    Then update your original code:

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // download the data in a background thread
    dispatch_async(queue, ^{
        CacheData *data = [Downloader getLatestData];
        **[AppCache updateCache:data];**
    
        // use the downloaded data in the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CacheUpdated" object:nil];
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, I'm tryning to extract data from web page using C#.. for the moment
Folks, is there a collection of gotchas where Numpy differs from python, points that
Folks, I tired all my PHP skills to extract domain name strings from a
folks! I have Service, which checks in onStartCommand() whether auto update was set in
Folks, Used to be that when you hit a servlet/jsp, the app server would
Folks, I have an asmx web sevice being hotes in IIS 7 with only
Folks, Debugging a .net 4.0 app using WinDbg (I'm a beginner to WinDbg). I'm
Folks, Please does anyone know how to show a Form from an otherwise invisible
Folks, what is the best way to kill an established connection from the shell
Folks - I'm trying to learn how to write OO Javascript, I come from

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.