Question

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
Was it helpful?

Solution

#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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top