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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:46:30+00:00 2026-06-14T01:46:30+00:00

In my iOS program the following happens: As the user types, a request is

  • 0

In my iOS program the following happens: As the user types, a request is fired off to a thread where a database lookup is initiated. When the DB lookup is done, a response is fired off on the main thread so the app can display the results.

This works great, except that if the user types really fast, there may be several requests in-flight. Eventually the system will catch up, but it seems inefficient.

Is there a neat way to implement it so that if a request is initiated, I can detect that a lookup is already in progress, and the request should instead be stored as “potentially newest that superceeds the one in-flight”?

SAMPLE SOLUTION WITH COMMENTS ADDED BELOW

Here’s the body of a view controller for a small sample project that illustrates the properties of the solution. When typing you might get output like this:

2012-11-11 11:50:20.595 TestNsOperation[1168:c07] Queueing with 'd'
2012-11-11 11:50:20.899 TestNsOperation[1168:c07] Queueing with 'de'
2012-11-11 11:50:21.147 TestNsOperation[1168:c07] Queueing with 'det'
2012-11-11 11:50:21.371 TestNsOperation[1168:c07] Queueing with 'dett'
2012-11-11 11:50:21.599 TestNsOperation[1168:1b03] Skipped as out of date with 'd'
2012-11-11 11:50:22.605 TestNsOperation[1168:c07] Up to date with 'dett'

In this case, the first enqueued operation is skipped because it determines that it has become outdated while performing the lengthy part of its work. The two following enqueued operations (‘de’ and ‘det’) are cancelled before they are even allowed to execute. The final final operation is the only one to actually finish all its work.

If you comment out the [self.lookupQueue cancelAllOperations] line, you get this behavior instead:

2012-11-11 11:55:56.454 TestNsOperation[1221:c07] Queueing with 'd'
2012-11-11 11:55:56.517 TestNsOperation[1221:c07] Queueing with 'de'
2012-11-11 11:55:56.668 TestNsOperation[1221:c07] Queueing with 'det'
2012-11-11 11:55:56.818 TestNsOperation[1221:c07] Queueing with 'dett'
2012-11-11 11:55:56.868 TestNsOperation[1221:c07] Queueing with 'dette'
2012-11-11 11:55:57.458 TestNsOperation[1221:1c03] Skipped as out of date with 'd'
2012-11-11 11:55:58.461 TestNsOperation[1221:4303] Skipped as out of date with 'de'
2012-11-11 11:55:59.464 TestNsOperation[1221:1c03] Skipped as out of date with 'det'
2012-11-11 11:56:00.467 TestNsOperation[1221:4303] Skipped as out of date with 'dett'
2012-11-11 11:56:01.470 TestNsOperation[1221:c07] Up to date with 'dette'

In this case, all the enqueued operation will perform the length part of their work, even though a newer operation has been enqueued after it before it has even been scheduled for execution.

@interface SGPTViewController ()

@property (nonatomic, strong) NSString* oldText;
@property (strong) NSOperationQueue *lookupQueue;

@end

@implementation SGPTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.oldText = self.source.text;
    self.lookupQueue = [[NSOperationQueue alloc] init];
    self.lookupQueue.maxConcurrentOperationCount = 1;
}

- (void)textViewDidChange:(UITextView *)textView
{
    // avoid having a strong reference to self in the operation queue
    SGPTViewController * __weak blockSelf = self;

    // you can cancel existing operations here if you want
    [self.lookupQueue cancelAllOperations];

    NSString *outsideTextAsItWasWhenStarted = [NSString stringWithString:self.source.text];
    NSLog(@"Queueing with '%@'", outsideTextAsItWasWhenStarted);
    [self.lookupQueue addOperationWithBlock:^{
        // do stuff
        NSString *textAsItWasWhenStarted = [NSString stringWithString:outsideTextAsItWasWhenStarted];
        [NSThread sleepForTimeInterval:1.0];
        if (blockSelf.lookupQueue.operationCount == 1) {
            // do more stuff if there is only one operation on the queue,
            // i.e. this one. Operations are removed when they are completed or cancelled.
            // I should be canceled or up to date at this stage
            dispatch_sync(dispatch_get_main_queue(), ^{
                if (![textAsItWasWhenStarted isEqualToString:self.source.text]) {
                    NSLog(@"NOT up to date with '%@'", textAsItWasWhenStarted);
                } else {
                    NSLog(@"Up to date with '%@'", textAsItWasWhenStarted);
                }
            });
        } else {
            NSLog(@"Skipped as out of date with '%@'", textAsItWasWhenStarted);
        }
    }];
}
  • 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-14T01:46:32+00:00Added an answer on June 14, 2026 at 1:46 am

    I like NSOperationQueue for situations like this.

    @interface ClassName ()
    ...
    // atomic since it does not specify nonatomic
    @property (strong) NSOperationQueue *lookupQueue;
    ...
    @end
    
    - (id)init
    {
        ...
        lookupQueue = [[NSOperationQueue alloc] init];
        lookupQueue.maxConcurrentOperationCount = 1;
        ...
    }
    
    - (void)textFieldDidChange
    {
        // avoid having a strong reference to self in the operation queue
        ClassName * __weak blockSelf = self;
    
        // you can cancel existing operations here if you want
        // [lookupQueue cancelAllOperations];
    
        [lookupQueue addOperationWithBlock:^{
            // do stuff
            ...
            if (blockSelf.lookupQueue.operationCount == 1) {
                // do more stuff if there is only one operation on the queue,
                // i.e. this one. Operations are removed when they are completed or cancelled.
            }
        }];
    }
    

    Edit: Just a note, you need to use [[NSOperationQueue mainQueue] addOperationWithBlock:] or similar to update the GUI or run any other code that must go on the main thread, from inside the block argument to [lookupQueue addOperationWithBlock:].

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

Sidebar

Related Questions

I have done the following to get record for user info from address book.
I am trying to program a converter in iOS. The user enters values into
When running my iOS program on an iPad, the console gives the following error
I have a ios program for iPad. It is used 2 view, A view
I’m running an iOS program in the 4.3.2, 5.0, and 5.1 Simulators, and I’m
While renewing iOS Developer program I have found Apple generated some Company ID instead
We are coming up to our iOS Developer Program renewal date and I want
I have enrolled in the iOS developer's program. I've developed an app which I
I'm working on a voice-controlled program for iOS and using Pocketsphinx as my recognition
My iOS app sends MIDI bank and program changes to other devices using PGMidi

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.