0

I am going to get Data for my application through Internet Object by Object and i want that each time the data has been updated or any object is added the current class that is using that class it gets notified that the data has been Updated. Currently i have reached here

#import <Foundation/Foundation.h>

@interface MSMutableArray : NSMutableArray
- (void) addObjectInArray:(id)anObject;
@end

then the Implementation class

#import "MSMutableArray.h"

@implementation MSMutableArray
- (void) addObjectInArray:(id)anObject
{
    [self addObject:anObject];
//  Notify the current Class that an element has been added????
}
@end
Quamber Ali
  • 2,170
  • 25
  • 46
  • 2
    By doing [KVO on NSMutableArray](http://stackoverflow.com/questions/302365/observing-an-nsmutablearray-for-insertion-removal) you can observe a particular mutable array instance for insertion or deletion of elements. – Amar Jun 24 '13 at 09:55
  • 1
    check out http://streetsaheadllc.com/article/key-value-observing-nsarray-and-nsdictionary – iCoder Jun 24 '13 at 09:57

1 Answers1

1
#import "MSMutableArray.h"

@implementation MSMutableArray
- (void) addObjectInArray:(id)anObject
{
    [self addObject:anObject];
//  Notify the current Class that an element has been added
[[NSNotificationCenter defaultCenter]postNotification:@"arrayUpdated withObject:nil]
}
@end

In the class using the array.

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(myMethod):...

-(void)myMethod{
//do stuff here
}

It may be better to use notification (as explained above) or create a delegate for the class doing the network request and when the data comes in; update the array and then send the message. I don't think it is necessary /borderline bad design to teach the array to do this. It breaks the cohesion principle of a class design

Quamber Ali
  • 2,170
  • 25
  • 46
William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • The post and addObserver methods aren't fully finished, but I noted that it is very close pseudocode. Autocomplete should finish it for you – William Falcon Jun 24 '13 at 11:13