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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:36:39+00:00 2026-06-15T05:36:39+00:00

My program uses Coredata (SQLite), NSPersistentDocument, NSTableView and an (entity) NSArrayController. I want to

  • 0

My program uses Coredata (SQLite), NSPersistentDocument, NSTableView and an (entity) NSArrayController. I want to have the NSTableView’s columns in the Main thread bound to the entity NSArrayController that I have populated in a Secondary Thread.

Question 1: Is it possible?. Unfortunately is not working in my case (while doing everything in the same thread through IB works)

What’s the objective: let the “fetch’s” (big document average is 2-4 secs to finish) run in a secondary thread so I can show a progress indicator on the UI while fetching.

Question 2: Is there any other recommended way os showing a progress indicator while the entity nsarraycontroller is arranging its data, fetching, etc…?

Thanks in advance.
Luis

// ------- ABCoredataController.h
@interface ABCoredataController : NSObject {
:
    NSArrayController               *ivArrayController;
}
@property (nonatomic, assign) NSArrayController  *arrayController;


// ------- ABCoredataController.m

// This piece executes in Main thread... 
- (void) init {
    ivArrayController = [[NSArrayController alloc] init];
:


// Following is later executed in the Secondary Thread
- (void) secondaryThreadRun:(id)param {
    :
    // prepare everything to access coredata from a secondary thread...
    [self setSecondaryThreadMOC: [[[NSManagedObjectContext alloc]init] autorelease] ];
    [[self secondaryThreadMOC] setPersistentStoreCoordinator:[self mainThreadPSC]];

    // prepare the (entity) array controller
    [[self arrayController] setAvoidsEmptySelection:YES];
    [[self arrayController] setPreservesSelection:YES];
    [[self arrayController] setSelectsInsertedObjects:YES];
    [[self arrayController] setClearsFilterPredicateOnInsertion:YES];
    [[self arrayController] setAutomaticallyPreparesContent:YES];
    [[self arrayController] setAutomaticallyRearrangesObjects:YES];
    [[self arrayController] setAlwaysUsesMultipleValuesMarker:NO];
    [[self arrayController] setUsesLazyFetching:NO];
    [[self arrayController] setEditable:YES];
    [[self arrayController] setEntityName:@"Transaction"];

    // bind arrayController to the managedObjectContext   
    [[self arrayController] setManagedObjectContext:[self secondaryThreadMOC]];
    [[self arrayController] setFilterPredicate:[self predicate]];

    :

Then inside the class where I control my XIB and all the UI…

// ------- ABWindowController.m
:
// Start the secondaryThreadRun in previous class
[[self coredataCtrlTransaction] start];
// Get the pointer to the entity array controller !!! <== HERE!! is it right?
ivOut_CtEn_Transaction = [[self coredataCtrlTransaction]arrayController];

:
// Bind that entity array controller to the NSTableView columns...
if ( [self out_CtEn_Transaction] != nil ) {
    for ( NSTableColumn *column in [[self out_Tableview_Transaction] tableColumns] ) {
        if ( [column identifier] != nil ) {
            if ( [column infoForBinding:@"value"] == nil ) {
                NSString *theKeyPath=nil;
                if ( [[column identifier] length] > 4 )
                    theKeyPath = [[column identifier] substringFromIndex:4];
                else
                    theKeyPath = [column identifier];

                [column bind: @"value" toObject: [self out_CtEn_Transaction]
                 withKeyPath:[NSString stringWithFormat:@"arrangedObjects.%@", theKeyPath] options:nil];
            }
        }
    }
}
  • 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-15T05:36:40+00:00Added an answer on June 15, 2026 at 5:36 am

    Answering myself, I’ve discovered that KVO is no good for interthread communication, I’m setting the observer in the MainThread, but the observation is received on the thread that originates the key value change (Secondary thread where the nsarraycontroller lives).

    So, if my background thread changes a value, my background thread is going to receive the KVO about it. Which is something I don’t want.

    Found good comment about it here:

    I’ve found another way of achieving my objective, much more simple.

    Found very good examples on GDC here and here

    My objective was: let me show an spinning wheel while my “nsarraycontroller is fetching or arranging is objects”, which in my case means 2-3 seconds.

        // Last step in the preparation of the predicate
        NSPredicate  *predicadoFinal = nil;
        predicadoFinal = [NSCompoundPredicate andPredicateWithSubpredicates:array_AND_Total];
    
        // Use GCD (Grand Central Dispatch) to be able to show the spinning wheel
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // Do here my stuff of showing in the UI something... 
                [[self out_ABSpinning_DDBB] setHidden:NO];
                [[self out_ABSpinning_DDBB] startAnimation:self];
            });
            // And here CPU consuming stuff. 
            // Apply the predicate to the nsarraycontroller. 
            // As the controller has both setAutomaticallyPreparesContent:YES
            // and setAutomaticallyRearrangesObjects:YES so setting 
            // the predicate will automatically trigger the controller
            // to process the predicate and fetch again... 
            [self setPredicateFINAL:predicadoFinal];
        });
    

    How do I stop the spinning wheel. It’s also easy, I’ve setup an observer on the entity NSArrayController like this:

    if (nil == ivObservableKeysABCtEn_Transaction ) {
        ivObservableKeysABCtEn_Transaction = [[NSSet alloc] initWithObjects:
                                              @"arrangedObjects",
                                              nil];
    }
    :
    
    if ( [self out_CtEn_Transaction] != nil ) {
    
        for (NSString *keyPath in [self observableKeysABCtEn_Transaction]) {
    
            // Añado observers para cada uno de los keyPaths en los que estoy interesado
            [[self out_CtEn_Transaction] addObserver:self
                         forKeyPath:keyPath
                            options:0
                            context:ABObserverABCtEn_Transaction];
        }
    }
    

    And then in:

    - (void)observeValueForKeyPath:(NSString *)keyPath
                          ofObject:(id)object
                            change:(NSDictionary *)change
                           context:(void *)context
    {
        // Fetch del objeto que ha cambiado en el emisor. En mi caso un BOOL
        id newChange = [change objectForKey:NSKeyValueChangeNewKey];
    
        // Detect null's
        if ([NSNull null] == (NSNull*)newChange) {
            newChange = nil;
        } else {
        :
            //
            // Somthing has changed in the "arrangedObjects" property
            // of my coredata array controller, so it has definitely
            // finished doing its work. 
            if ( context == ABObserverABCtEn_Transaction ) {
               [[self out_ABSpinning_DDBB] stopAnimation:self];
               [[self out_ABSpinning_DDBB] setHidden:YES];
    

    Thanks
    Luis

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

Sidebar

Related Questions

I have a program that uses pthread library to do the matrix multiplication of
I have a program that uses: ThreadPool.QueueUserWorkItem(new WaitCallback(FireAttackProc), fireResult); On Windows7 and Vista it
My main program uses a variable that stores the pointer to a byte. How
I have various constants that my program uses... string 's, int 's, double 's,
I have a program that defines a variable int data The program uses scanf(%d,&data)
I have a java program using an external library. The main program uses log4j
I have a program that can be ran several times. The program uses a
My program uses wx.ProgressDialog to give feedback on a process that is in multiple
My program uses some external programs that i included in the Files folder. In
In the following, how can I make it such that the program uses 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.