Re: objects and "global / shared data"
Re: objects and "global / shared data"
- Subject: Re: objects and "global / shared data"
- From: Allyn Bauer <email@hidden>
- Date: Tue, 03 May 2011 17:22:28 -0500
(replying to all this time)..
A singleton may be an appropriate solution to this problem. Their usage is not perfect but few tools are.
That said, "DataHandler" should probably not be the singleton. That is the class you created to read the file. If it doesn't make sense to create another object to model your data (and it sounds like it may not - if it is an array. It depends on what it is an array of), then you can store this array in your application delegate and use [[UIApplication sharedApplication] delegate] to get access to it that way.
The other thing to keep in mind is you might indeed not need a singleton. For example, if you have a simple navigation hierarchy as such:
List of objects -> object details
Then you can get the data from your DataHandler and load it into the list of objects controller. When the user selects the thing, you simply pass the object around and it is updated. Storing the array of items in this scenario in a singleton/as a singleton is incorrect, and eventually you will wish you hadn't.
Basically,
>
> But the menu is created in a different class than the one managing the tableview.
Probably means, only create the menu once, and pass the pointer around. :)
Hope that helps,
Allyn
On May 3, 2011, at 5:08 PM, Martin Batholdy wrote:
> Hi,
>
> I am still new to objective-c and cocoa and the whole object-oriented approach.
>
> So perhaps this is a weird question for you, but for me it is really important to understand.
>
> I have a small program with a DataHandler-class.
> This class has methods to get data from a file on the disk and to save it.
>
> Actually it reads a plist containing an Array.
>
> Now I need this Array at different places in my program.
> I want to display it in a menu and in a tableView.
>
> But the menu is created in a different class than the one managing the tableview.
>
> However both classes need the data from the DataHandler.
>
> So they separately initiate their own instance of DataHandler.
> Now the problem is that when one class changes something in the array the other class doesn't know about that,
> because it has its own instance of the DataHandler.
>
> Now how can I make sure that a global data-source is available independent from instances and their state?
> How can I share data between objects?
>
> I read something about singletons and that by declaring the DataHandler object a singleton I can make sure that over the whole program only one DataHandler-instance is initiated,
> and the actual Data-Array is the same in each instance.
>
> But I also read that singletons are bad practice.
>
> But how can I share data (an Array) over different objects?
>
>
> I still don't get this point ...
>
> Or do I have to use CoreData?
>
>
>
>
> _______________________________________________
>
> 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
_______________________________________________
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