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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:37:23+00:00 2026-06-05T07:37:23+00:00

One thing that I do is: -(void)GrabbingProcess:(void (^)())block; { self.OtherGrabbingIndicator +=1; block(); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2

  • 0

One thing that I do is:

-(void)GrabbingProcess:(void (^)())block;
{
    self.OtherGrabbingIndicator +=1;
    block();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        self.OtherGrabbingIndicator -=1;
    });
}

Everytime I want to run something that may take a long time in the background, I do something like

[Grabclass grab] GrabbingProcess: ^{
//Do something grab data etc.
}];

There are many such functions and many such data. For example, at first I would grab all the businesses ID. Then I would grab all the details on the businesses on separate thread.

I want to know a time when ALL those threads have finished and post a suitable notification.

The problem with my solution is that after a while, quite often the value of self.OtherGrabbingIndicator hover around 2 or 3 never going down even though all of those thread have finished.

Somehow some of the self.OtherGrabbingIndicator +=1; “leaked” and not matched with self.OtherGrabbingIndicator -=1. I wonder how can that leak ever happen?

  • 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-05T07:37:26+00:00Added an answer on June 5, 2026 at 7:37 am

    If you want to run blocks asynchronously and then find out when they’re done, the appropriate tool is a dispatch_group. You can dispatch blocks onto a specific group and queue using dispatch_group_async(), and the group will track when the block is finished. You can then either wait synchronously on the group to finish with dispatch_group_wait() or register a block to be invoked when the group is done with dispatch_group_notify().


    The reason why your solution above doesn’t work is because you aren’t accessing/mutating the counter in a threadsafe way. Here’s a trivial series of events that will cause the problem:

    Start: self.OtherGrabbingIndicator is 1
    Thread A: reads self.OtherGrabbingIndicator
    Thread B: reads self.OtherGrabbingIndicator
    Thread A: increments the read value and writes back to self.OtherGrabbingIndicator
    Thread B: increments the read value and writes back to self.OtherGrabbingIndicator
    End: self.OtherGrabbingIndicator is 2

    Even though two threads tried to increment it, only one “succeeded” and you end up losing the other increment. This can also happen during decrement. If you use dispatch groups this problem goes away.


    As a side note, you should never use dispatch_get_current_queue(). This is a debugging function that is never supposed to be used in actual code. The primary reason is because you aren’t just running on one queue, you’re running on a whole hierarchy of queues simultaneously, but this function can only tell you about the one queue. In addition, you may be running on someone’s private queue and you certainly should not be dispatching onto that yourself.

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

Sidebar

Related Questions

One thing that bothers me about nHibernate is that it is not 100% compile
One thing that really bothers me about Google Code is that fact that it
One thing that's bothered me with vi is that paste (p) inserts lines after
One thing that seems particularly easy to do in the Flash IDE but difficult
I'm learning F# and one thing that preoccupies me about this language is performance.
I am relatively new to Xcode and one thing that has bothered me is
I am new to using numpy and one thing that I really don't understand
I used to work in JavaScript a lot and one thing that really bothered
I started learning html recently, and one thing that really confused me is why
Coming from a C++ background, one thing that confuses me about Objective C is

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.