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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:04:30+00:00 2026-06-08T02:04:30+00:00

In iOS I need to call same method number of time, but the app

  • 0

In iOS I need to call same method number of time, but the app has to wait until the method completed it’s first task. (I can not use bool value to check whether the function is running or not).

How can I queue the request or wait until it’s previous task has finished? Is there a way I can use NSOperation or NSThread?

Thanks

This is my method I need to call multiple times

- (void)fetchJobs
{

    dispatch_queue_t queueLocal = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t groupLocal = dispatch_group_create();

    UIApplication *app = [UIApplication sharedApplication];

    NSTimeInterval backgroundTime = app.backgroundTimeRemaining;
    NSLog(@"Background task remain time: %g seconds", (double)backgroundTime);


    [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_START" object:nil];

    self.assetUpdateTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{

            [self endTask];
        }); 
    }];

    dispatch_group_async(groupLocal, queueLocal, ^{

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Files" inManagedObjectContext:self.managedObjectContext];

        //Setup the fetch request
        NSFetchRequest *request = [[NSFetchRequest alloc] init];
        [request setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat: @"uploaded like %@", [NSString stringWithFormat:@"0"]];
        [request setPredicate:predicate];

        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

        NSError *error = nil;

        //fetch the records and handle an error
        NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];


        if (fetchResults == nil) {
            NSLog(@"File Data fetching error : %@", [error localizedDescription]);
        }
        else{
            if ([fetchResults count] > 0) {            

                dispatch_apply([fetchResults count], queue, ^(size_t i){
                    NSLog(@"Loop count %zu", i);

                    Files *file = [fetchResults objectAtIndex:i]; 
                    self.manageObjectForFiles = file;
                    NSLog(@"Fetched File details -> Name: %@ & status: %@" , file.fileName, file.uploaded);                    

                    BOOL uploaded = [self filePosting:file.fileName];


                    if (uploaded) {

                        [self.managedObjectContext deleteObject:file];
                        NSError *error;

                        if (![self.managedObjectContext save:&error]) {
                            NSLog(@"File table update error : %@", [error description]);
                        }
                    }                     

                });
                /*
                for (int i = 0; i < [fetchResults count]; i++) {                    

                }*/
            }            
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

            int remainJobs = [self fetchFileTableCount];
            if (remainJobs > 0) {

                [UIApplication sharedApplication].applicationIconBadgeNumber = remainJobs;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_REMAIN" object:nil];
            }
            else{
                [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
                [[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOAD_PROCESS_COMPLETED" object:nil];
                NSLog(@"-----------------------------------------------");
                NSLog(@"There are no more uploads");
                NSLog(@"-----------------------------------------------");
            }
            [self endTask];                                    

        });

    });

    dispatch_release(groupLocal);

}
  • 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-08T02:04:32+00:00Added an answer on June 8, 2026 at 2:04 am

    You’d didn’t say if the calls have to be serial of if they can be concurrent. If concurrent you can use NSOperation’s mainQueue, suspend it, add all but the first call, then in your method, at the end, see if the queue is suspended and if so resume it.

    With Grand Central Dispatch (CGD) you can more easily create a serial dispatch queue, and sort of do the same – suspend it, add all but the first call wrapped in a block, then at the end of the first call unsuspend the queue. If you need this to run on the main thread you can have the serial queue tied to the main GCD queue.

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

Sidebar

Related Questions

I need to add ability to call certain number/account in my iOS app. But
My iOS app has a need to uniquely identify each device so it can
I need to call [[MKLocationManager sharedLocationManager] _applyChinaLocationShift:newLocation] in my iOS app. I believe MKLocationManager
I need to implement XML-RPC call in my ios app. I've just came across
I need to know which iOS device is currently running app (saying more exactly
I need to generate .rtf files in my Monotouch developed iOS app. I've found
I need to use a plist from an existing iOS app in my android
I need to develop a native app both on iOS and Android. It is
Need help with a problem. Goal I'm putting together an iOS book app that
Our company created an iOS app that has an iPhone version and an iPad

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.