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];
            }
        }
    }
}
Luis Palacios
  • 396
  • 1
  • 3
  • 14

1 Answers1

0

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

Community
  • 1
  • 1
Luis Palacios
  • 396
  • 1
  • 3
  • 14