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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:21:16+00:00 2026-06-13T08:21:16+00:00

I want to provide read/write access to class property in side a timer’s event

  • 0

I want to provide read/write access to class property in side a timer’s event handler but also update the same property elsewhere in the class outside the event handler. What precautions do I need to take to make sure that the correct data is being read & updated?

Here’s the general logic:

// declared in the class header and initialized to 1 in init
@property (nonatomic, strong) NSNumber           *sharedItem;
@property (nonatomic, assign) dispatch_source_t  timer;

// Method invoked independent of the timer
- (void)doSomeWork {
    // QUESTION: During a timer tick, will it access the correct version of 
    // sharedItem that is updated here? 
    // Do I need to protect this area with a critical section/lock?
    sharedItem = [NSNumber numberWithInteger:[sharedItem intValue] + 1];
}

- (void)myTimerRelatedMethod {

    // Creating the timer
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(self.timer, startTime, interval, leeway);

    // Timer's event handler run for each tick
    dispatch_source_set_event_handler(self.timer, ^{
        if ([sharedItem intValue] > 10) {
            // 1. Do something 
            // 2. Then cancel the timer
        }
    });

    dispatch_resume(self.timer);
}
  • 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-13T08:21:17+00:00Added an answer on June 13, 2026 at 8:21 am

    For simple primitives you can just set the property to atomic, and you dont have to worry about read write inconsistencies between threads.

    For pointers you should use @synchronize to avoid read write consistencies in addition to setting the property to atomic.

    Also note that if your timer is in the same thread as the rest of your code (on the main thread+runloop), you dont need to do anything, as the timer event will be fired by the same runloop firing in the rest of the main thread code, and will not be truly concurrent.

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

Sidebar

Related Questions

This question has been already asked, but never answered. I want to write some
I want to provide some functionality through an extension. This requires (read and execute)
I want to write a program in C/C++ that will dynamically read a web
I want sell some .NET library and I want provide edition with full source
I want to provide a button which cancels the present task, Let's say I
I want to provide my user with some meaningful error messages when network requests
I want to provide my colleagues with an interface (using Windows Forms or WPF)
I want to provide the ability to save some rendered data/charts (with the titles
I want to provide a custom Batcher for Hibernate to use (for this reason:
I want to provide for partial matching, so I am tacking on * to

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.