I am trying to understand how to implement true Cocoa MVC like in Figure 4-7 in this link.
Here's a link
Figure 4-7 shows that model uses pattern observer to notify controllers (passive MVC). I implement observer in my model in such way
@interface IADataManager : NSObject
//MARK: Parsed Feed With IANewsDataObj
@property (nonatomic,retain) NSMutableArray *feedArray;
//MARK: Model use Singelton Pattern
+ (IADataManager *) sharedInstance;
//MARK: Observation methods
- (void) addListener:(id<IADataManagerListener>) listener;
- (void) removeListener:(id<IADataManagerListener>) listener;
//MARK: Business Logic
- (void) loadFeedFromNetwork;
- (void) loadFeedFromDataBase;
- (void) loadImageForTarget:(id<IADataManagerListener>) target
AtIndexPath:(NSIndexPath *) indPath;
- (void) saveFeedToDataBase;
@end
@protocol IADataManagerListener <NSObject>
- (void) IADataManager :(IADataManager *) dataMng
didRefreshWithError :(NSError *) error;
- (void) IADataManager :(IADataManager *) dataMng
didLoadImageForIndexPath :(NSIndexPath *) indexPath;
- (void) IADataManager :(IADataManager *) dataMng
didLoadWithError :(NSError *) error;
@end
- (void) addListener:(id<IADataManagerListener>) listener
{
if([self.listeners indexOfObject:listener] == NSNotFound && listener)
[self.listeners addObject:listener];
}
- (void) removeListener:(id<IADataManagerListener>) listener
{
if([self.listeners indexOfObject:listener] !=NSNotFound && listener)
[self.listeners removeObject:listener];
}
//Notification example
- (void) handleLoadedNews:(NSArray *) loadedNews
{
[self.feedArray addObjectsFromArray:loadedNews];
[self.listeners enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
objc_msgSend(obj, @selector(IADataManager:didRefreshWithError:),self,nil);
}];
}
I wonder, if there are better way to implement observer in the model? For example using KVO or NSNotificationCenter. But problem is that with the help of KVO and NSNotificationCenter i can not use selectors with more than 1 argument. For example,
- (void) DataManager:(DataManager *) dm withObj1:(Obj1*) obj1 Obj1:(Obj2*) obj2 Obj3:(Obj3*) obj3
Thanks a lot!