MVC: Controller keeps array of model objects or view objects? - objective-c

I have a view controller placeViewController which pulls in a bunch of Place objects, and from these loads and populates a matching bunch of PlaceView objects which are then displayed as subviews to its main view.
I need to update these views periodically which requires information from the model and other places to be pushed into the views
How should I properly structure this?
Keep an array of Place objects and give the model a PlaceView property to store pointers to the views
Keep an array of PlaceView objects and give the view a Place property to store pointers to the places
Keep both arrays and use a lot of indexOfObject objectAtIndex to jump between them
Some other way??

I need to update these views periodically which requires information
from the model and other places to be pushed into the views
You should really focus on an ObserverPattern. Yours views register to "notifications", and when changes are made, your registered views are notified.
http://en.wikipedia.org/wiki/Observer_pattern
You still can use NSNotificationCenter, but it will not futfill your needs since you need yours views make update per Place object (paired like a dictionary). So i recommend you to implement your own.
It will loose coupled because your controller will just only make glue between the observer and your views, and all the "logic" will be handled by the observer.

Well basvk got the answer in his comment but hasn't posted an actual answer for me to mark correct.
So here it is: "I would create a NSMutableArray with PlaceView objects. And the PlaceView class holds a #property Place *place"

Related

best practices for archiving/unarchiving NSTableViewController data?

I have an NSWindowViewController that owns a nib file. One of the objects in the nib file is an NSTableView, managed by an NSTableViewController (also part of nib).
I want to implement the NSCoding protocol to save off all model data, some of which is contained within the NSTableViewController (which I'll refer to as TVC for short). Archiving the entire TVC seems like the wrong answer, since when its unarchived later, it won't be wired up to the nib. And while I could probably rewire everything programmatically...that just seems very kludgy.
I could have the NSWindowViewController grab the table data out of the TVC, but that would mean I would have to expose all of the ivars, which also seems like the wrong solution.
Whats the best way of archiving/unarchiving the data stored within the TVC?
The table view controller should have a reference to the model that stores you data, or if your app is simple you might have that model data stored in an array or dictionary in the table view controller. It is that array or dictionary, whatever holds your data that should be archived, not the table view controller.

iOS ARC with when two objects need a pointer to the same thing

I'm going through my code to make sure that all of my properties have the proper weak/strong modifier and I ran across this situation which I don't know what to do with. Keep in mind that I'm fairly new to iOS programming.
I have a normal MVC hierarchy where my controller creates an object called FieldManager. This FieldManager is used by my controller to dynamically create textfields. However, this FieldManager also needs to be used by the controller's model to periodically query the manager to find out information about the fields (such as is it required, should it's text be capitalized...etc).
So to summarize, I have a controller which creates an object that is used both by the controller and the controller's model. Therefore, I don't know if I should make the model's reference to FieldManager a weak property or the controller's reference to it a weak property. It seems that I should make both weak properties, otherwise FieldManager will get released. What should I do?
Thanks.
Things like that should belong to your model, so the way to go is to have a datasource.
Your controller asks the datasource to create and return the textfields, the datasource contacts the model and asks for a field manager for that model.
That's the way I would do it...

Where to create my objects? Multiple views in project

I have a project that will have a lot of views (20 in total). They are displayed in sequence and the user makes certain decisions before tapping a button to make the next view appear. When all 20 views have been displayed, it goes back to the first view.
I also need to create a lot of instances of 3 different objects. So there may be 40 different instances of object1, 20 of object2, and 30 of object3. Each view needs to know about all of these instances and will often change instance variables.
I need to pass these instances between the views. I think I will do this by passing a pointer along to the next view. My question (I've finally got there), is where to create all these instances in the first place? In the original View Controller? Or in the AppDelegate?
Many thanks for any pointers (pun intended)
Usually you'd use CoreData for this. You start creating objects in initial view controller. Then after user manipulates the object you pass that object into next view controller (probably via UINavigationController) and create appropriate objects there as needed and so on and so forth.
On a side note - please rethink your navigation flow and user experience. User might be tired enough after as little as 5th configuration view.
Think MVC
When designing the model, consider diferent aproaches: CoreData, serialization in filesystem, in memory using singletone...
And remember: load lazily

How do I keep track of the data represented by a view?

I'm attempting to do a better job of keeping my data and my visual representation of data separate in a new project and I'm having some trouble with the abstraction.
I have a list of objects in a set that are part of an inventory and I use that data to generate some UIImageViews that get placed into the interface, thus keeping my models and views separate. When a user taps on the UIImageView, I want to be able to take an action on the data in the set as well as modifying the corresponding view.
How do I keep track of the object that is being represented by the UIImageView? I would think there's some sort of mechanism by which I can say "here's a reference to the object that you represent" but I can't figure out if that exists or if there's a more accepted way to achieve the view/model separation.
you could create and object that is an extension of the ImageView and upon initialization of that view, associate a property in the view to your data associated with the view.

Core Data: When and where are Entities loaded in the first Place?

I have a question about Core Data. When starting my appliction, when is my data (which is stored automatically by Core Data) loaded into the NSArrayControllers? I want to modify it in the first place before the user can interact with it.
To be more specific: I have an NSArrayController for the entitity Playlist. Before the user can add new playlists or interact with the app at all, I want to modify the playlists programmatically. I tried windowControllerDidLoadNib: in my NSPersistentDocument (MyDocument.m) and awakeFromNib both in my NSPersistendDocument and the NSArrayController, but when I check in these methods with [[myArrayController arrangedObjects] count] I get 0 as result (the array controller's content is empty).
However, I actually have data stored and it is displayed to the user. I just do not know when and where I can modify it in the first place.
Thank your for any help.
Data is never "loaded" into the NSArrayController. The array controller is not an array itself. It does not contain or otherwise store data.
Instead, the array controller queries the object it is bound to for specific pieces of data only when that specific data is needed. This is especially true of Core Data in which managed objects are only fully instantiated when their attributes are accessed. The array controller moves data from an array type data structure to another object (usually an UI element.)
If you want to modify an existing store before it displays in the UI, you need to process the data before the array controller used by the UI is even initialized. If you're using NSPersistentDocument, then you can override readFromURL:ofType:error: to fetch and modify all your objects when the document is first opened. Alternatively, you can override the window controller's windowWillLoad or showWindow methods.
Regardless of where you do it, you must fetch all the managed objects you want to modify. You could programmatically create an array controller to do this but a fetch request is easier to micro manage if you have a large number of objects to modify.
You could try observing the "arrangedObjects" keypath of the controller and adding some logic to work that your array controller has been populated for the first time.
Another possible hook is implementing the awakeFromInsert/awakeFromFetch methods of your managed objects.