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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:02:04+00:00 2026-06-09T12:02:04+00:00

I need to do the following tasks: 1) Read some data from sqlite database

  • 0

I need to do the following tasks:

1) Read some data from sqlite database

2) Process the data

3) With the processed data generate some charts

If I have a user who enters many data in the application, one day this analisys can become slow and freeze the UI.

So, what is the correct way to process it allowing the user to interact with the UI, with the option to cancel the operation or exit the screen?

I need to make simple threads for all my tasks and with a cancel event or flag to stop each one? Or there is another way do to it?

For example:

Task 1: Read the data from sqlite in a thread with a flag to stop the process if needed.

Task 2: Process the data in a thread with a flag to stop the process if needed.

Task 3: Deliver the data to a 3rd party component. At this point, its possible to cancel the operation that is running on other component?

Am I thinking the right way or I could improve on something?

  • 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-09T12:02:06+00:00Added an answer on June 9, 2026 at 12:02 pm

    This is the recommended and fastest way by Apple with GCD (Grand Central Dispatch). It´s also easier to read and understand, because the logic is linear but not split between methods.

    Note that it shows the weakSelf “dance” which is necessary if i.e. the async may outlive the controller it is called for, thus it makes a weak reference to it and then checks if it is alive and retains it to update:

    Swift 4 & 3

     DispatchQueue.global().async() {
          print("Work Dispatched")
          // Do heavy or time consuming work
    
          // Then return the work on the main thread and update the UI
          // Create weak reference to self so that the block will not prevent it to be deallocated before the block is called.
          DispatchQueue.main.async() {
               [weak self] in
               // Return data and update on the main thread, all UI calls should be on the main thread 
               // Create strong reference to the weakSelf inside the block so that it´s not released while the block is running
               guard let strongSelf = self else {return}
               strongSelf.method()
          }
     }
    

    Objective-C

    // To prevent retain cycles call back by weak reference
       __weak __typeof(self) weakSelf = self;  // New C99 uses __typeof(..)
    
        // Heavy work dispatched to a separate thread
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"Work Dispatched");
            // Do heavy or time consuming work
            // Task 1: Read the data from sqlite
            // Task 2: Process the data with a flag to stop the process if needed (only if this takes very long and may be cancelled often).
    
            // Create strong reference to the weakSelf inside the block so that it´s not released while the block is running
            __typeof(weakSelf) strongSelf = weakSelf;
            if (strongSelf) {
    
                [strongSelf method];
    
                // When finished call back on the main thread:
                dispatch_async(dispatch_get_main_queue(), ^{
                    // Return data and update on the main thread
                    // Task 3: Deliver the data to a 3rd party component (always do this on the main thread, especially UI).
                });
            }
        });
    

    The way to cancel the process is to include a BOOL value and set it to stop from the main thread if the work being done is no longer needed. But, it may not be worth it because the user will not notice the background work so much unless it´s heavy calculations.
    To prevent retain cycles use weak variables like:

    __weak __typeof(self) weakSelf = self; // Obj-C
    [weak self] in
    

    and call weakSelf with strong reference inside the block (to prevent crashes if the calling VC has been released). You can use the exact type like UIViewController or the __typeof() function (in C99 you need to use __typeof(..) but previously you could use __typeof(..) directly) to refer to the actual type:

    Objective-C

    __typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf) {
       [strongSelf method];
    }
    

    Swift 4 & 3

    if let weakSelf = self {
       weakSelf.method()
    }
    

    or

    // If not referring to self in method calls.
    self?.method()
    

    NOTE: Use GCD or Grand Central Dispatch, it´s most simple, the recommended way by Apple and the code flow is in logical order.

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

Sidebar

Related Questions

Please read Update1 first! This is SSIS specific question. I have the following tasks:
I have the following database table consisting of a series of tasks: id BIGINT
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
Colleagues, Using JPA I need resolve following issue: at database level exits 3 entities
I need to execute from Java a batch script, which does following 1) Once
Need some assistance with my Visual Studio projects and in particular with release process.
I have an SQL table Tasks with columns Id and State . I need
I have cca 25 databases which I need to consolidate into 1 database. First
I have a task archive some database tables. To simplify everything, I basically have
I need following namespaces to use native wpf property grid however, VS 2010 doesn't

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.