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

  • Home
  • SEARCH
  • 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 879995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:01:30+00:00 2026-05-15T12:01:30+00:00

Early warning – code sample a little long… I have a singleton NSMutableArray that

  • 0

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.

  • 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-05-15T12:01:30+00:00Added an answer on May 15, 2026 at 12:01 pm

    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.

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

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are two things to be gained by using an… May 17, 2026 at 2:26 am
  • Editorial Team
    Editorial Team added an answer They are not the same thing. A FooList is a… May 17, 2026 at 2:26 am
  • Editorial Team
    Editorial Team added an answer The common solution for this in SQL is to create… May 17, 2026 at 2:26 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Warning: This is inherited legacy code that was initially put together in the early
I have a list of strings that are all early modern English words ending
I'm at the very early stage of learning Android. Can anyone tell me how
I have just jumped into Objective-C and I got stuck pretty early on. I
I realize that the query this question is looking for won't be enough to
I get this warning but cannot find an explicit reference to the mentioned assembly.
ORIGINAL MESSAGE (now outdated): After running python setup.py install I get the following: Warning:
I'm working on a project at the moment that we're using junit to test,
I have a class with 4 arrays: tab_1,tab_2,tab_3 and tab_T. I set the picker's
I am trying to use an early experimental release of mapper implementation to empty

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.