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.
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.
Related
What is the best way to create a local databas I swift and fetch data from it when an IBAction is tapped. Lets say I want to make a recipe app and when I click on "Food1" an IBAction with a unique key I sent to the database and it will populate the "RecipeDetailView" from the databas.
Im note sure on which way to go. Should I use a thridparty database manger like Realm or can I just create the whole databas in Swift.
The database should contain stuff like a local image, title, description etc. And I want to connect the data to labels, textviews and image view.
What you are describing seems like you want to work with Core Data, Apple's built in persistence framework. For more information look at the Core Data programming guide
There are loads of tutorials and books about Core Data. Look at the Getting Started with Core Data Tutorial on raywenderlich.com
I am starting a new project (learning purposes) and I am trying to figure out what is the best software design pattern to use in the following scenario.
I have several data that need to be downloaded from multiple webservices and store somewhere in my app, to display it later. However each piece of data (e.g. list of teachers, students) will only be used in one or more specific view controllers (e.g. teachersViewController and studentsViewController).
I read that the Singleton pattern or use the AppDelegate to store a variable (an object like ApplicationData) is a bad practise, even more in this example which I want to restrict the data access.
So, which design pattern should I choose? I have read something about dependency injection, but I don't have any clue about it or if it even helps me in this question. If it helps, some examples with explanation would be nice.
You need some sort of database to store downloaded data. Good choices are Realm and Core Data. The right way to process data is:
Check if data is already in DB and show it if available.
Download or update data from server and parse it to objects.
Save objects to DB.
Show data taken from DB to user.
Download data as needed. When you open VC with students then download only students data and so on.
EDITED: If you need all the data on app open then load it and put in a DB before first screen opens. Then just use DB to show data to user.
So i have no idea how to manipulate data effectively. Currently I am querying all my data (messages) from my backend and manipulating it within the view controller file and passing it among view controllers via segues. I realize this is probably ridiculous for a messaging app.
I am thinking of making a separate file called data in order to manipulate the data effectively. The data consists of the message body, object id, sender name, sender id, and time stamp. I was also planning on making custom classes to handle this data.
Would it be best to take this route and how do I effectively manipulate data from other view controllers?
You should use SQLite to save all data.You can create a new table and query all data.It's easy to learn and use.Good luck.
I'm currently writing my first iOS app and I'm trying to implement Core Data. Users create food recipes and I want them saved to persistent storage.
Currently, without Core Data, I'm putting the recipes into an array from a singleton.
When I implement Core Data can I do away with this and fetch straight from the data model every time I need to update my UITableView? In the viewDidLoad method, create an array from the current data in Core Data every time a user navigates to it?
Yes you can.
You should look at the class NSFetchedResultsController it is designed to do this.
There is a great Ray Wenderlich tutorial which covers everything including things like automatically updating the table when the core data model changes etc...
app scenario: on the UI, a button is tapped to get contact list from the server. the request goes to subproject which does the download and parsing and returns the result thru its delegate to the UI. so far everything works properly. lets say there is no internet connection and we cant have the contact list. to solve the problem, I want to cache the data in core data. if there is no internet, the cached data will be returned. now the question that bugs me, is it possible to create one data model and use it in subproject to save the data and in UI where data get pulled and edit from the same data model?
so basically i want to access core data from different subprojects and UI.
i couldnt find hints or tutorials regarding this issue. any ideas?
thanks in advance!
edit:
a project "b" that is added to the parent project "a". the project "b" is actually a static library.
if i let the library to do the saving and returning data to UI, wont it be inefficient to get all data from core data then send it to the UI?
i actually hope that there is a way to use same data model in both UI and the library.
i want prevent the UI to have huge load of data. its better to hace core data to handle that incl. memory mangement. i'm still reading some sources and trying to implement it on a test project.
I would argue that only the main project should deal with persistency, as than you can always decide to handle it differently — save it permanently or not, use core data or a home grown sql wrapper…. So it would be up the the delegate to decide what to do with more data.
But along with the delegate protocol you could decide to maintain different model protocols that define, what your models can hold. this would be independent to the implementation. The delegate now could return objects — no matter if core data models or not — to the delegator if this objects conforms to the protocols. The delegator in the sub module now could check for values on the server and/or in the cache.