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];
}
}
}
}
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.
How do I stop the spinning wheel. It’s also easy, I’ve setup an observer on the entity NSArrayController like this:
And then in:
Thanks
Luis