Re: Newbie Object Sharing Question
Re: Newbie Object Sharing Question
- Subject: Re: Newbie Object Sharing Question
- From: Brian Slick <email@hidden>
- Date: Sat, 31 Jan 2009 11:31:53 -0500
On Jan 30, 2009, at 9:47 PM, Ken Thomases wrote:
On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:
It starts to occur to me that I don't actually want an instance, I
want the real deal.
That statement is nonsensical. There is no "real deal". An
instance is real. It's not some pale reflection of something else.
You do want an instance.
I suspect that perhaps you don't understand the difference between
an object and a pointer to an object. The reason I suspect that is
because the simple solution to your dilemma, the thing you're not
seeing, is that you want a single instance referenced from multiple
places using multiple pointers.
It may be a nonsensical statement here (good to know), but in the 3D
CAD world that I'm used to, which I have been leveraging in my
attempts to understand OOP, there is a distinction between instances
and the source item.
And I believe that I do understand the difference between objects and
pointers (I could be mistaken). The part I'm missing is what this
needs to look like in code. I'm afraid I need some handholding here.
If I knew what I needed to do in order for there to be only a single
data object referenced from multiple places, I wouldn't have asked the
question in the first place.
I read through some documentation about model objects and objects
in general, and stumbled upon the concept of Singletons. Some
additional searching lead me to this blog post:
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want. I reconfigured
MyListItemArray as a singleton, and remapped my data source
methods accordingly, and everything seemingly works perfectly.
Items added in one view are displayed in the other, and so on. It
works so well that I have to assume there is a catch.
I can't shake the feeling that this seems more difficult than in
ought to be, and generally when I feel that way there tends to be
a single line of code solution that I haven't found yet. Are
singletons really the only way (that doesn't involve saving to a
file, I suppose) to share a model object across multiple view
controllers? I think I'm missing something really fundamental.
Yes, I think your are missing something fundamental -- the ability
to share references to an object simply by assigning multiple
pointers to point to the same thing.
Yes, exactly. How do I do that?
A singleton implementation is _one_ solution to the issue you're
having, but it's not necessary, and not even recommended for
something as simple as this.
Can you expand upon why not? Based on the blog post I linked, it
sounds like this is exactly the situation in which to do this.
One stumbling block is that you have two view controllers, but
evidently no central application controller. View controllers
should have logic and state specific to a view and that view's
relationship to the model. An application controller manages your
application overall. It manages the other controllers, including
your view controllers. It has the primary responsibility for
managing the application-global (as opposed to, for example,
document-specific) model, and for providing access to that model to
other parts of the program.
Well, I just used the Tab Bar Application template, and other than the
AppDelegate that doesn't contain very much, there is no provided
application controller. The need to create one was not immediately
obvious to me, as none of the examples in the book I'm reading through
have dealt with accessing the same data set in different tabs.
I also do not yet see how this solves my problem. I need to see some
code. From my view controllers, I don't believe that I know how to
send messages to the AppDelegate (say, for the table data source
methods) without creating an instance, which I believe brings me back
around to the same problem I started with.
If your application has one central list of items, then it would be
the application controller's job to create and load that model. It
would hold references to the model objects in instance variables.
It would provide access to those model objects to other parts of the
program. One way would be to pass the references in to the view
controllers when those view controllers are initialized. Another
way would be for the application controller to expose those
references through properties and have the view controllers
reference those properties -- the view controllers would need a
reference to the application controller to do that. Again, they
could get such a reference by being passed it, or if your
application controller is your application delegate, they can obtain
the reference using [NSApp delegate].
Regards,
Ken
Thank you for the explanation, but I'm afraid I still have no idea
what I specifically need to do, or even what help topic to go search
on for additional instruction. The basic stuff about messaging I have
found so far all deals with creating new instances to send messages to.
Brian
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden