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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:13:37+00:00 2026-06-03T07:13:37+00:00

In my model I have an array of objects called events. I would like

  • 0

In my model I have an array of objects called events. I would like my controller to be notified whenever a new object is added to events.

I thought that a good way to do this would be use the KVO pattern to get notified when the events changes (from a new object being added)

// AppDelegate
// events is a NSMutableArray @property/@synthesize etc...

[appDelagate addObserver:self
               forKeyPath:@"events"
                  options:NSKeyValueObservingOptionNew
                  context:NULL];

But the observeValueForKeyPath method wasn’t being called and I discovered that Arrays are not KVO compliant 🙁

One option is to manually trigger the method by calling willChangeValueForKey for the keyPath

// ViewController
[self willChangeValueForKey:@"events"];
[self.events addObject:event];
[self didChangeValueForKey:@"events"];

But this feels heavy since I should probably also keep track of the before and after state of my events array so that it can be accessed from the observeValueForKeyPath method.

One approach could be to use a standard array (instead of mutable) and create/set a new instance of events each time I want to add an new object, or I could make a separate property that keeps track of how many items are in the mutable array (I wish you could observe @”events.count” ).

Another option would be to use NSNotificationCenter. I’ve also read some answers that suggest using blocks (but I have no idea where to start on that).

Finally, could I keep an instance of my controller in my delegate and just send a relevant message?

// Delegate
[myController eventsDidChange];

Is it odd to keep a reference to a controller from a delegate?

I’m struggling to understand how to choose which is the best approach to use, so any advice on performance, future code flexibility and best practices is greatly 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-06-03T07:13:39+00:00Added an answer on June 3, 2026 at 7:13 am

    You should not make direct public properties for mutable collections to avoid them mutating without your knowledge. NSArray is not key-value observable itself, but your one-to-many property @"events" is. Here’s how to observe it:

    First, declare a public property for an immutable collection:

    @interface Model
    @property (nonatomic, copy) NSArray *events;
    @end
    

    Then in your implementation back it with a mutable ivar:

    @interface Model ()
    {
        NSMutableArray *_events;
    }
    @end
    

    and override the getter and setter:

    @implementation Model
    
    @synthesize events = _events;
    
    - (NSArray *)events
    {
        return [_events copy];
    }
    
    - (void)setEvents:(NSArray *)events
    {
        if ([_events isEqualToArray:events] == NO)
        {
            _events = [events mutableCopy];
        }
    }
    
    @end
    

    If other objects need to add events to your model, they can obtain a mutable proxy object by calling -[Model mutableArrayValueForKey:@"events"].

    NSMutableArray *events = [modelInstance mutableArrayValueForKey:@"events"];
    [events addObject:newEvent];
    

    This will trigger KVO notifications by setting the property with a new collection each time. For better performance and more granular control, implement the rest of the array accessors.

    See also: Observing an NSMutableArray for insertion/removal.

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

Sidebar

Related Questions

I have this widget: $this->setWidget('slug', new sfWidgetFormDoctrineChoice(array('model' => 'MyTable', 'method' => 'myMethod', 'key_method' =>
I have a CoreData model (managed object) called Item: @interface Item : NSManagedObject {
I have an object model called MyObject that I'm serializing to a json string
So I have a array of records and I would like to group by
Let's say I have a model called User . I have an array with
I have a tree controller bound to an array, called content. content is an
In my model I have three complex scopes returning the Discount object arrays. Each
I have a model that I'm trying to retrieve an array of data from,
I'm using ASP.NET MVC and have a model which has an image (byte array)
Please have a look at this validation array in my cakephp app for model

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.