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

  • Home
  • SEARCH
  • 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 6340843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:54:47+00:00 2026-05-24T19:54:47+00:00

I’ve a multi-threading application in which each thread has to do some job, but

  • 0

I’ve a multi-threading application in which each thread has to do some job, but at a certain point some code needs to be executed serially (like writing into sqlite3 database), so I’m calling that code to be performed on main thread using:

[self performSelectorOnMainThread:@selector(serialJob:) withObject:object waitUntilDone:YES];

and every thing went just fine except that when that code needs some time the user interaction with the application gets disabled until that code has been finished, so is there any way to make another ONE thread that can be run on background and can be called whenever I need it just like the main one so I can replace the previous call with:

[self performSelector:@selector(serialJob:) onThread:REQUIRED_THREAD withObject:object waitUntilDone:YES];

this thread should be some class’s static data member to be accessed from all over the code.

any help would be very appreciated, and many thanks in advance…

  • 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-24T19:54:47+00:00Added an answer on May 24, 2026 at 7:54 pm

    Due to the my question that I need the current thread to be blocked until the database job has been finished, I’ve tried these two solutions and they worked perfectly. You can either use critical sections or NSOperationQueue and I prefer the first one, here is the code for both of them:

    define some class “DatabaseController” and add this code to its implementation:

    static NSString * DatabaseLock = nil;
    + (void)initialize {
        [super initialize];
        DatabaseLock = [[NSString alloc] initWithString:@"Database-Lock"];
    }
    + (NSString *)databaseLock {
        return DatabaseLock;
    }
    
    - (void)writeToDatabase1 {
        @synchronized ([DatabaseController databaseLock]) {
            // Code that writes to an sqlite3 database goes here...
        }
    }
    - (void)writeToDatabase2 {
        @synchronized ([DatabaseController databaseLock]) {
            // Code that writes to an sqlite3 database goes here...
        }
    }
    

    OR to use the NSOperationQueue you can use:

    static NSOperationQueue * DatabaseQueue = nil;
    + (void)initialize {
        [super initialize];
    
        DatabaseQueue = [[NSOperationQueue alloc] init];
        [DatabaseQueue setMaxConcurrentOperationCount:1];
    }
    + (NSOperationQueue *)databaseQueue {
        return DatabaseQueue;
    }
    
    - (void)writeToDatabase {
        NSInvocationOperation * operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(FUNCTION_THAT_WRITES_TO_DATABASE) object:nil];
        [operation setQueuePriority:NSOperationQueuePriorityHigh];
        [[DatabaseController databaseQueue] addOperations:[NSArray arrayWithObject:operation] waitUntilFinished:YES];
        [operation release];
    }
    

    these two solutions block the current thread until the writing to database is finished which you may consider in most of the cases.

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

Sidebar

Related Questions

No related questions found

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.