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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:32+00:00 2026-05-27T13:43:32+00:00

My app delegate needs to unlock/lock a purchase order on my microsoft SQL database

  • 0

My app delegate needs to unlock/lock a purchase order on my microsoft SQL database when it enters the background or terminates.

Here’s my code

    - (void)applicationWillResignActive:(UIApplication *)application {

    NSLog(@"App will enter background");
    if ([purchaseOrderIsLocked boolValue] && [purchaseOrderAutoID intValue] > 0) {
        NSLog(@"Unlock Purchase Order because app is in background %i", [purchaseOrderAutoID intValue]);
        [self unlockPurchaseOrderWithAutoID:self.purchaseOrderAutoID];
    }   

- (void)applicationWillEnterForeground:(UIApplication *)application {

   NSLog(@"App will enter foreground");

    if ([purchaseOrderIsLocked boolValue] == FALSE && [purchaseOrderAutoID intValue] > 0) {
        NSLog(@"Lock Purchase Order because app is coming back %i", [purchaseOrderAutoID intValue]);
        [self lockPurchaseOrderWithAutoID:self.purchaseOrderAutoID];
    }
}

-(void)lockPurchaseOrderWithAutoID:(NSNumber *)autoID {

NSLog(@"lock purchase order called with autoID=%i",[autoID intValue]);

    self.client = [SqlClientConnect connect];
    [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '9999' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] 
          withCompletionBlock:^(SqlClientQuery *query) {

              if (query.succeeded) {
                  NSLog(@"locked purchase order");
                  purchaseOrderIsLocked = [NSNumber numberWithBool:YES];
              } else {
                  [self queryFailedWithError:query.errorText]; 
              } 
          }];
}

-(void)unlockPurchaseOrderWithAutoID:(NSNumber *)autoID {
    NSLog(@"unlock purchase order called with autoID=%i",[autoID intValue]);

    self.client = [SqlClientConnect connect];
    [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '0' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] 
          withCompletionBlock:^(SqlClientQuery *query) {

              if (query.succeeded) {
                  NSLog(@"unlocked purchase order");
                  purchaseOrderIsLocked = [NSNumber numberWithBool:FALSE];
              } else {
                  [self queryFailedWithError:query.errorText]; 
              } 
          }];
   }

}

Okay it looks good to me… however when I run the app this is what I get from the log when I press the home button

    2011-12-14 13:41:20.558 MA Mobile[2267:707] App will enter background
    2011-12-14 13:41:20.558 MA Mobile[2267:707] Unlock Purchase Order
     because app is in background 18 

2011-12-14 13:41:20.559 MA Mobile[2267:707] unlock purchase order called with autoID=18

It should have gone all the way and posted “unlocked purchase order” but it doesn’t
When I resume the app this is what I get.

2011-12-14 13:41:44.741 MA Mobile[2267:707] App will enter foreground
1 2011-12-14 13:41:44.973 MA Mobile[2267:707] unlocked purchase order

It’s like it runs the block code after resuming. Is this related to the block? Should I do something different?

  • 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-05-27T13:43:33+00:00Added an answer on May 27, 2026 at 1:43 pm

    As soon as you pass off the block and return to -applicationDidResignActive:, your app is immediately suspended and the asynchronous call you started is essentially paused. You need to let the application know you need to spend more time in the background using -beginBackgroundTaskWithExpirationHandler:. You can do it with code like the example below. For more information on long running background tasks, see Background Execution and Multitasking in the iOS App Programming Guide.

    This simple example shows how to use this feature in your code:

    -(void)unlockPurchaseOrderWithAutoID:(NSNumber *)autoID
    {
      NSLog(@"unlock purchase order called with autoID=%i",[autoID intValue]);
    
      // Start long-running background task
      UIBackgroundTaskIdentifier bti = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    
      self.client = [SqlClientConnect connect];
      [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '0' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] withCompletionBlock:^(SqlClientQuery *query) {
        if (query.succeeded) {
            NSLog(@"unlocked purchase order");
            purchaseOrderIsLocked = [NSNumber numberWithBool:FALSE];
        } else {
            [self queryFailedWithError:query.errorText]; 
        }
        // After we complete the asynchronous block, we need to let the system know we can suspend now
        if( bti != UIBackgroundTaskInvalid )
          [[UIApplication sharedApplication] endBackgroundTask:bti];
      }];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code stub for my app-delegate.m -- it never gets called. -
I have an app that needs to do something when it’s sent to background
my program needs to take a prepopulated sql database and then save the records
I'm using the following Audio Session in my app delegate: AudioSessionInitialize(NULL, NULL, NULL, self);
I'm writing a Cocoa application where I have an auto generated app delegate:(MyAppDelegate.h/MyAppDelegate.m) So
When my app is run in the iPhone simulator, the delegate method - (void)applicationWillTerminate:(UIApplication
I am writing an iphone app where in numerous cases a subview needs to
I am using following functions in my App delegate - (void)applicationWillResignActive:(UIApplication *)application { NSLog(@applicationWillResignActive);
I've made a weather application which currently needs to use your location in order
My app needs to play some audio files and I want to fade-out the

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.