Avoid communication in a document-based Cocoa application?

I am new to Mac programming and am working on a documented application.

My subclass of NSDocument creates a subclass of NSWindowController . This window controller also creates two subclasses of NSViewController.

Sometimes a change in one of the NSViewController must notify the NSDocument and / or the main model class. In addition, a model change must be notified of all / some of the submissions.

My question is: what's the best approach so that there is no (or minimal) grip? I know that there are several options, but I'm not sure which one is better for my application, since I'm not a newbie for programming, but for Cocoa and especially NSDocument :

  • KVO. It looks nice and easy to implement, but I don’t like the idea of ​​not explicitly notifying the observer (s) of the change (AFAIK, self.someProperty = newValue does notify observers automatically), and I don’t like the fact that you have to register in property names that may change over time.

  • Notifications. I know what it is, and I used them for iOS. But I read somewhere that they cannot be immediately sent to observers. It's true? If not, do you see them as a good approach for a document-based application?

  • Delegates Yes, under normal circumstances (or, what I usually saw) the class has one delegate. But creating an array of delegates also works (just tested). The problem I see here is that every time I need to notify delegates, I have to iterate over them so that they respond to the method and call that method.

Are there any other alternatives that I am missing?

+4
source share
2 answers

The KVO controller class is the most common way of interacting between a model and its presentation. In fact, Cocoa bindings, which are designed to eliminate code in the main controller layer, are based on KVO. It is true that KVO / KVC relies on property names, and if these changes change, you will have to change the bindings or KVO setting that connects your view. However, it is usually not possible to make your ideas completely unaware of the specifics of the base model, so I don’t see a problem in this.

My recommendation would be to use Cocoa Binding where you can, as they eliminate a lot of glue code. In places where they cannot be used, your controllers (middle layer in MVC) should use KVO to monitor model changes and update the corresponding views. Changes to views can be reverted to the model through property accessors and / or KVCs using controllers.

+1
source

Yes, in normal conditions (or, what I usually saw), the class is one delegate. But creating an array of delegates also works (just tested it).

Delegates are often used to change the behavior of a delegating object. A good example is application delegation: NSApplication itself is not very interesting; he relies on his delegate to determine the interesting behavior of the application. Having multiple delegates trying to change the behavior of a single object can be a problem if different delegates conflict with each other. What will you do if delegates disagree?

There are several cases in Cocoa when a class uses more than one delegate, but each of them has a separate role. For example, NSTableView has both a delegate and a data source, but both of them are really delegates of the sort.

The problem that I see here is that every time I need to notify delegates, I have to go through them, make sure they respond to the method and call this method.

This is not difficult to solve. For example, you can create an NSInvocation to encapsulate a call, and then send a call to each delegate. However, if you do, you have almost reinvented the notification system. If you require a one-to-many message that you receive with a suggestion from several delegates, you will probably be better off using notifications or KVO.

+1
source

Source: https://habr.com/ru/post/1391661/


All Articles