views:

85

answers:

1

Early warning - code sample a little long...

I have a singleton NSMutableArray that can be accessed from anywhere within my application. I want to be able to reference the NSMutableArray from multiple NIB files but bind to UI elements via NSArrayController objects. Initial creation is not a problem. I can reference the singleton NSMutableArray when the NIB gets loaded and everything appears fine.

However, changing the NSMutableArray by adding or removing objects does not kick off KVO to update the NSArrayController instances. I realize that "changing behind the controller's back" is considered a no-go part of Cocoa-land, but I don't see any other way of programmatically updating the NSMutableArray and letting every NSArrayController be notified (except it doesn't work of course...).

I have simplified classes below to explain.

Simplified singleton class header:

@interface MyGlobals : NSObject {
    NSMutableArray * globalArray;
}

@property (nonatomic, retain) NSMutableArray * globalArray;

Simplified singleton method:

static MyGlobals *sharedMyGlobals = nil;

@implementation MyGlobals

@synthesize globalArray;

+(MyGlobals*)sharedDataManager {
    @synchronized(self) {
    if (sharedMyGlobals == nil)
    [[[self alloc] init] autorelease];
}

return sharedMyGlobals;
}

-(id) init {
if(self = [super init]) {
        self.globals = [[NSMutableArray alloc] init];
    }
    return self
}

// ---- allocWithZone, copyWithZone etc clipped from example ----

In this simplified example the header and model for objects in the array:

Header file:

@interface MyModel : NSObject {
NSInteger myId;
NSString * myName;
}

@property (readwrite) NSInteger myId;
@property (readwrite, copy) NSString * myName;

-(id)initWithObjectId:(NSInteger)newId objectName:(NSString *)newName;

@end

Method file:

@implementation MyModel

@synthesize myId;
@synthesize myName;

-(id)init {

[super init];

myName  = @"New Object Name";
myId    = 0;

return self;
}

@end

Now imagine two NIB files with appropriate NSArrayController instances. We'll call them myArrayControllerInNibOne and myArrayControllerInNib2. Each array controller in the init of the NIB controller sets the content of the array:

// In NIB one init
[myArrayControllerInNibOne setContent: [[MyGlobals sharedMyGlobals].globalArray];

// In NIB two init
[myArrayControllerInNibTwo setContent: [[MyGlobals sharedMyGlobals].globalArray];

When each NIB initializes the NSArrayController binds correctly to the shared array and I can see the array content in the UI as you would expect. I have a separate background thread that updates the global array when content changes based on an external event. When objects need to be added in this background thread, I simply add them to the array as follows:

[[[MyGlobals sharedMyGlobals].globalArray] addObject:theNewObject];

This is where things fall apart. I can't call a willChangeValueForKey and didChangeValueForKey on the global array because the shared instance doesn't have a key value (should I be adding this in the singleton class?)

I could fire off an NSNotification and catch that in the NIB controller and either do a [myArrayControllerInNibOne rearrangeObjects]; or set the content to nil and reassign the content to the array - but both of these seems like hacks and. moreover, setting the NSArrayController to nil and then back to the global array causes a visual flash within the UI as the content is cleared and re-populated.

I know I could add directly to the NSArrayController and the array gets updated, but I don't see a) how the other NSArrayController instances would be updated and b) I don't want to tie my background thread class explicitly to a NIB instance (nor should I have to).

I think the correct approach is to either fire off the KVO notification somehow around the addObject in the background thread, or add something to the object that is being stored in the global array. But I'm at a loss.

As a point of note I am NOT using Core Data.

Any help or assistance would be very much appreciated.

A: 

Early warning - answer a little long…

Use objects that model your domain. You have no need for singletons or globals, you need a regular instance of a regular class. What Objects are your storing in your global array? Create a class that represents that part of your model.

If you use an NSMutableArray as storage it should be internal to your class and not visible to outside objects. eg if you are modelling a zoo, don't do

[[[MyGlobals sharedMyGlobals].globalArray] addObject:tomTheZebra];

do do

[doc addAnimal:tomTheZebra];

Dont try to observe a mutable array - you want to observe a to-many property of your object. eg. instead of

[[[MyGlobals sharedMyGlobals].globalArray] addObserver:_controller]

you want

[doc addObserver:_controller forKeyPath:@"animals" options:0 context:nil];

where doc is kvo compliant for the to-many property 'anaimals'.

To make doc kvo compliant you would need to implement these methods (Note - you don't need all these. Some are optional but better for performance)

- (NSArray *)animals;
- (NSUInteger)countOfAnimals;
- (id)objectInAnimalsAtIndex:(NSUInteger)i; 
- (id)AnimalsAtIndexes:(NSIndexSet *)ix;
- (void)insertObject:(id)val inAnimalsAtIndex:(NSUInteger)i;
- (void)insertAnimals:atIndexes:(NSIndexSet *)ix;
- (void)removeObjectFromAnimalsAtIndex:(NSUInteger)i;
- (void)removeAnimalsAtIndexes:(NSIndexSet *)ix;
- (void)replaceObjectInAnimalsAtIndex:(NSUInteger)i withObject:(id)val;
- (void)replaceAnimalsAtIndexes:(NSIndexSet *)ix withAnimals:(NSArray *)vals;

Ok, that looks pretty scary but it's not that bad, like i said you don't need them all. See here. These methods dont need to be part of the interface to your model, you could just add:-

- (void)addAnimal:(id)val;
- (void)removeAnimal:(id)val;

and write them in terms of the kvc accessors. The key point is it's not the array that sends notifications when it is changed, the array is just the storage behind the scenes, it is your model class that send the notifications that objects have been added or removed.

You may need to restructure your app. You may need to forget about NSArrayController altogether.

Aaaaaannnnnyyywaaayyy… all this gets you nothing if you do this

[[[MyGlobals sharedMyGlobals].globalArray] addObject:theNewObject];

or this [doc addAnimal:tomTheZebra];

from a background thread. You can't do this. NSMutableArray isn't thread safe. If it seems to work then the best that will happen is that the kvo/binding notification is delivered on the background as well, meaning that you will try to update your GUI on the background, which you absolutely cannot do. Making the array static does not help in any way i'm afraid - you must come up with a strategy for this.. the simplest way is performSelectorOnMainThread but beyond that is another question entirely. Threading is hard.

And about that static array - just stop using static, you don't need it. Not because you have 2 nibs, 2 windows or anything. You have an instance that represents your model and pass a pointer to that to you viewControllers, windowControllers, whatever. Not having singletons/static variables helps enormously with testing, which of course you should be doing.

mustISignUp
mustISignUp - Thanks for your reply. In answer to your question, lets say I have a "person" model which has a name, age, date of birth etc. I created a class we will call "employees". I moved the NSMutableArray out from the singleton into "employees" and made it a static NSMutableArray. Adding objects to the array is done by a call into "employees" e.g. [employeeInstance addNewEmployee:@"George" withAge: 22 gender:@"male"]. In "employees" it calls [personInstance initWithName:newName age:newAge gender:newGender]. Where would I add the addObserver KVO?
Hooligancat
Surely there should be a KVC that gets triggered by the "person" model when the object is added to the array (I'll call it employeesArray) or at least it should get fired in the "employees" class where the employeesArray lives? I have implemented the countOfEmployeesArray, objectInEmployeesArrayAtIndex, insertObject, removeObjectFromEmployeesArrayAtIndex, replaceObjectInEmployeesArrayAtINdex and addEmployeesArrayObject accessor methods to the "employees" class and still the KVC doesn't get fired. I need a static because I need to reference the same array from multiple NIB files and UI controls
Hooligancat
Oh - one more thing... if I'm accessing the mutable array as a static does it not get locked until any changes have been made thus negating the threading issue?
Hooligancat
ok, i've expanded a little. Hope it helps. Let me know how it goes.
mustISignUp
mustISignUp - I do appreciate the help. This seems to come down to two issues. Let me tackle the first problem - storing a mutable array of information in memory without having a shared array in the global space or a static mutable array in the class. Am I missing something fundamental. How can you reference the same mutable array if every time you instantiate an instance of the class you are not referencing something on the global or static level? If I had two instances of the class without a static or shared I would have two mutable arrays - each oblivious of the other...
Hooligancat
Admittedly this doesn't have much to do with your ArrayController problem, but i started it so.. Having a static variable is just not the object-orientated way to do things. If i have a Zoo class, and i make two instances, bronxZoo and londonZoo, adding a giraffe to londonZoo shouldn't add an object to bronxZoo, should it? That would be a side-effect http://bit.ly/bPmbpt. They should be as you say 'oblivious of the other' - so why do you think that is a problem? It seems to be because you want to GET this value (giraffe) from elsewhere in your code, when you should be TELLING other objects.
mustISignUp
mustISignUp - I agree - we are off topic here. Dropping the zoo issue on this question is appropriate. I can create a new question which addresses the static issue :-) So.. notifying the NSArrayController of updates to the number of items in an NSMutableArray. At some level, the NSMutableArray changes. I can capture the point in time when that occurs and have a KVC event fire saying "hey we updated". I could use KVO to listen for the event in the controller that has the NSArrayController, but setting the content to nil and then setting the content back the the NSMutableArray seems like a hack.
Hooligancat
mustISignUp - Compromised. I went with your method but had the NSMutableArray as a static class instance. Wrapped the setter in a lock to account for thread safety and then passed a pointer to each of the NSArrayController's. Seems to work reasonable well. Thanks.
Hooligancat