Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Sharing data among controller objects

Sharing data among controller objects
Thread Tools
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Sep 17, 2005, 04:43 PM
 
I've got an application that's designed as follows:

AppController is a subclass of NSObject that deals with the main interface,
PrefsController is also a subclass of NSObject that deals with the preferences window and maintains arrays of persistent custom objects that both Controllers need to access.
Both Controllers have instance variables pointing to each other.

Right now, I have it set up so that when AppController needs to get a list of, say, configured "gizmos," it uses [prefsController gizmos] to get it. When it has modified a gizmo and the changes need to be saved to disk, it does [prefsController saveGizmos], which tells PrefsController to put the gizmo array into NSSharedUserDefaults and synchronize the defaults, etc.

This seems odd and inefficient for some reason. Since both Controllers need access to the same set of objects, it seems like it would make more sense to have them both read from a shared store like NSSharedUserDefaults. But then changes in one aren't reflected in changes in the other for some reason, and the two sets are constantly out of sync, and they could easily overwrite each other's changes if I have them save to UserDefaults separately. So what is the best practice in this case?

Also, I'm wondering if it would be better to do something like having PrefsController listen for a GizmosHaveChanged notification, instead of being told directly to saveGizmos. When is it better to use indirect communication like NSNotifications instead of direct messaging?
     
techtrucker
Senior User
Join Date: Feb 2003
Location: USA
Status: Offline
Reply With Quote
Sep 19, 2005, 04:54 AM
 
I can't say what the ramifications are of my method, but in a somewhat similar situation I used notifications. Perhaps there's something technically "wrong" with doing it that way but it works exactly how I want it to.
MacBook 2.0 160/2GB/SuperDrive
Lots of older Macs
     
bewebste
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Sep 20, 2005, 12:47 AM
 
What I usually do is to have a separate Preferences object that has accessors that pass values to/from NSUserDefaults with setters that send out notifications that any relevant controllers can listen to. Then I just have a shared instance of Preferences that any object can get/set its values from. This setup also keeps with the MVC paradigm better than storing values right in the prefs controller.
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Sep 20, 2005, 04:02 PM
 
How do you make shared instances of things? I assume you mean shared as in something along the lines of [NSUserDefaultsController sharedUserDefaultsController], right?

Also, one reason I asked this question is because I'm thinking about moving control of the gizmo array to AppController instead of PrefsController. This doesn't make a lot of logical sense, but as things are:
1. The PrefsController is instantiated by the AppController upon init
2. The PrefsController loads the Preferences.nib for the window it controls during init or else the app crashes with a SIGSEV

I can't figure out why the app crashes unless I load the Preferences.nib. I'd like to delay loading the nib until the user explicitly opens the prefs window, but right now the only way to do that is to delay loading the PrefsController. But that means that AppController doesn't have access to the gizmos list.

I'm kind of rambling here, but do people understand what I'm getting at?
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 04:59 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,