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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:41:37+00:00 2026-06-01T12:41:37+00:00

According to Best way to remove from NSMutableArray while iterating? , we can’t remove

  • 0

According to Best way to remove from NSMutableArray while iterating?, we can’t remove an object from NSMutableArray while iterating, yes.

But, what if I have a code like the following

- (void)sendFeedback {
    NSMutableArray *sentFeedback = [NSMutableArray array];
    for (NSMutableDictionary *feedback in self.feedbackQueue){
        NSURL *url = [NSURL URLWithString:@"someApiUrl"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:[feedback objectForKey:@"data"] forKey:@"data"];
        [request setCompletionBlock:^{
            [sentFeedback addObject:feedback];
        }];
        [request startAsynchronous];
    }
    [self.feedbackQueue removeObjectsInArray:sentFeedback];
}

I’m using a NSRunLoop to create a NSThread to execute the sendFeedback method every a period of time. The way I sent data to the API is by using Asynchronous method (which will create a background thread for each request) Once the feedback has been sent, it has to be removed before NSRunner execute this method at the next period to avoid duplicate data submission.

By using asynchronous, the loop (the main thread) will continue running without waiting for the response from server. In some cases (maybe most cases), the loop will finish running before all the response from server of each request come back. If that so, the completion block’s code will be execute after the removeObjectsInArray which will result in sent data remains in self.feedbackQueue

I’m pretty sure that there are several ways to avoid that problem. But the only one that I can think of is using Synchronous method instead so that the removeObjectsInArray will not be execute before all the request’s response are come back (Either success or fail). But if I do so, it’s mean that the internet connection has to be available for longer period. The time needed to the sendFeedback’s thread will be longer. Even it will be run by newly created NSThread which will not cause the app to not respond, resources will be needed anyways.

So, is there any other way besides the one I mentioned above? Any suggestion are welcome.

Thank you.

  • 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-01T12:41:38+00:00Added an answer on June 1, 2026 at 12:41 pm

    There are a few ways to deal with this kind of problem. I suggest using a dispatch group to synchronize your feedback and using an instance variable to keep from executing a new feedback batch while one is still in progress. For this example, let’s assume you create an instance variable named _feedbackUploadInProgress to your class, you could rewrite your -sendFeedback method like this:

    - (void)sendFeedback
    {
      if( _feedbackUploadInProgress ) return;
      _feedbackUploadInProgress = YES;
    
      dispatch_group_t group = dispatch_group_create();
      NSMutableArray *sentFeedback = [NSMutableArray array];
      for (NSMutableDictionary *feedback in self.feedbackQueue) {
        // enter the group for each item we're uploading
        dispatch_group_enter(group);
        NSURL *url = [NSURL URLWithString:@"someApiUrl"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:[feedback objectForKey:@"data"] forKey:@"data"];
        [request setCompletionBlock:^{
          [sentFeedback addObject:feedback];
          // signal the group each time we complete one of the feedback items
          dispatch_group_leave(group);
        }];
        [request startAsynchronous];
      }
      // this next block will execute on the specified queue as soon as all the
      // requests complete
      dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.feedbackQueue removeObjectsInArray:sentFeedback];
        _feedbackUploadInProgress = NO;
        dispatch_release(group);
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

according to What's the best way to detect a 'touch screen' device using JavaScript?
What is the best way to access my managedObjectContext. According to this post I
I am looking for the best way to pipe data from one stream to
What is the best way to localise a date format descriptor? As anyone from
I would like to know the best way to determine if the following code
What is the best way to store a route according to all nice patterns?
What would be the best way to handle the __repr__() function for an object
What is the best way to render a data object into html? Take into
I've been declaring private methods in class extensions, according to Best way to define
According to What's the best way to communicate between view controllers? best practices for

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.