0

Im new to objective-c and I've been reading up on singleton classes. I want to implement it into my logic but im not sure if its correct/possible/doable to do so, any advise would be appreciated.

At the moment i'm loading data from an xml feed, but i want to have control what data should be displayed based on which button is clicked. For example, buttonA would display IT news and buttonB would display celebrity news.

My thinking is to load the xml data into sqlite on application start in the background and display my buttons view at the same time using the singleton class. If the user pushes the button it will query the required table and display the content into a tableView.

Is this viable? If not, could you please advise whats the best way to go about this?

Thank you.

david-l
  • 623
  • 1
  • 9
  • 20

1 Answers1

1

First of all you should reconsider organization of your data model. You've named sqlite on one hand and a global array on the other.

I would point you to Core Data to store your parsed data in a convenient way. Finally all you need is to query the Core Data db and fetch what you need. This would be more memory efficient than storing your data in a global array.

Have a look at Apple's Core Data tutorial or at this nice turorial: "superdb-core-data-app-with-sections"

To share a managed object context you can use a singleton. Have a look at this blog post, it provides a solution without a singleton by passing references of the managed object context down the relevant objects. It is created in the app delegate.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108
  • Sorry ive had another think about this and ive revised my question, i didnt expect to get an answer so quickly. Thank you. I am using core data but i want to manage the loading of the data to the database and ive been told singleton is probably the best place to start looking. – david-l Feb 26 '11 at 21:04
  • The example tutorial is exactly what im doing now straight from reading the xml feed but i have no control on how to extract the required based on the button pressed. Im looking at this as being the lower level but i need a top level that would get the data into my database in the first place. – david-l Feb 26 '11 at 21:11
  • I see what you mean. All you need to share between your view controllers, or whatever class you use to query your db, is a NSManagedObjectContext. You may see it as a link to your db. You could, of course, create it on application startup and make it accessible through a singleton. – Nick Weaver Feb 26 '11 at 21:54
  • I added a new link to the answer addressing the toppic how to "share" a managed object context. – Nick Weaver Feb 26 '11 at 21:57