1
votes

I am trying to understand how to implement true Cocoa MVC like in Figure 4-7 in this link.

Here's a link

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW1

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!

3

3 Answers

1
votes

But problem is that with the help of KVO and NSNotificationCenter i can not use selectors with more than 1 argument.

You can!!!

Store all the selectors in an array or dictionary and then pass that single object.

0
votes

I think use of delegation or block will be ideal one.To get callback in view controller you can use delegation method or block . I think that wlll be ideal approach for MVC design patterns.

0
votes

KVO is usually the better option. Example:

In IDataManager:

- (void) handleLoadedNews:(NSArray *) loadedNews
{
    [self willChangeValueForKey:@"feedsArray"]; //Also, see - (void)willChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key
    [self.feedsArray addObjectsFromArray:arr];
    [self didChangeValueForKey:@"feedsArray"]; //Also, see - (void)didChange:(NSKeyValueChange)changeKind valuesAtIndexes:(NSIndexSet *)indexes forKey:(NSString *)key
}

And, in ViewController:

- (void) viewDidLoad {
    ....
    [[IADataManager sharedInstance] addObserver:self forKeyPath:@"feedsArray" options:0 context:NULL];
}

- (void) viewWillUnload {
    ....
    [[IADataManager sharedInstance] removeObserver:self forKeyPath:@"feedsArray"];
}

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    //if keyPath is "feedsArray", refresh my views
}