Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my application, I want to have a "brain" that keeps track of what's going on. That is - multiple view controllers need to be able to set values in this brain and get at its data too.
How would I go about implementing this? From what I can tell, making my brain a singleton class is an option, otherwise I'd have to declare the brain as a delegate in every view controller and assign the brain to it every time it's created, which seems quite messy.
Your answer is in your question as you said. Use Singleton pattern if you want to access an object from multiple objects and there is no need for more than one copy.
Bear in mind that you must keep your data thread-safe if you will have two or more objects in your code that will try to manipulate the "brain" at the same time.
As for the Singleton pattern, you might like to read this What is so bad about singletons?
Make sure you design your app using the MVC pattern and you should be good. The "brain" is the model.
How your "model" behaves depends on your application.
Singleton pattern is an option. Another option is NSUserDefault.
Yes you are right Singleton is a good option.
As i think you are dealing with low amount of data so singleton will be good and easy otherwise go for saving data in a database or NSuserDefaults.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have been hearing that the controller should be as concise as possible. So, I try to keep most of the processing work in the helper for the corresponding controller. But, I am little confused regarding whether I should instead use private controller methods or helper methods.
I am using helper methods only for processing and returning values. They have no other usage for now. They are not called from views.
That's a bad idea in general. Helper methods shouldn't be used except for specific convert-something-into-string stuff (basically, should be used only in views).
For anything else, you should use service objects or something similar, basically, PORO (plain old ruby objects)
In controllers you want to handle authentication and render the right thing, you don't want to deal with anything else otherwise they become too complex.
You might want to check these books to improve knowledge about this topic:
Growing Rails applications which is a really good book on how to avoid putting code in wrong places and how to keep codebase maintenable on the long term
POODR which is a Ruby guide on how to write good Object-Oriented code, it's a must for any developer and will help you understand why using an helper it's a bad idea
Usually, a Helper is called in a template or a view.
If you want to keep your controller DRY, use functions in your models or in services (read this good article about services by Ben Lewis)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm writing an application which uses core data, and I use it to store a reasonable amount of user generated data.
However, the app also has a few settings ... such as the users name, age.
I'm wondering if it is better practice to store setting information in CoreData, or to simply store this information in UserDefaults?
answer acording to title: no
(added: especially not large amounts of binary data)
This is really more a question for Programmers Stack Exchange, but I give it a quick go over.
Core Data is powerful, but that power comes at a cost. It adds complexity and indirection.
I think it good to think of all your model objects separately. Figure out what makes the most sense for that object. This is not to say let chaos rule, patterns make code easier to understand. I think it's not good to shoehorn something into an existing pattern, because you want everything in your app to fit the pattern.
All that said. Unless your app is storing a list of users, I think it's better to use the simpler approach of NSUserDefaults.
Core Data is a big thing, not really simple. I would rather you to think about your data model first. If its "big" enough and you think about to expand it maybe in the future i would recommend you to use core data.
Now to my personal opinion:
I'm not really a fan of Core Data. I use mostly a SQlite-Database. If you need alot more and you are a starter checkout parse.com. Its a complete backend-service in verious languages. Check it out..
If you are saving credentials or some other protected data then this is the best practice:
iOS: How to store username/password within an app?
For temp data and most of other flat data use UserDefaults...
Use CoreData as more structural data storage, when you have lots of records or linked records, for more complex data structures in general...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have read several articles discussing pros an cons about singleton patterns. But I would like to know:
Is it advisable to have multiple singletons in an iOS App?
what are the pros and cons...?
Currently I am having only one singleton globally and holding strong references of other necessary properties including custom composite classes. But the idea sounds something strange for me for an example, accidentally I can create several instance of a custom composite class which I don't want.
You should have as many singletons as you need. Take a look at Cocos2d - it contains a fair amount of them: CCDirector, CCTextureCache, CCSpriteFrameCache and so on. There's no limit on singletons, say 5. If it's convenient for you to have one single center class for a certain kind of operations (like accessing network or a database or whatever) and you never need a second instance of this class then feel free to make it a singleton.
It depends on your requirement.
You can have multiple singleton classes or objects.
The singleton object will be alive till your application quits.
For memory managing concern, it'll be very difficult if you have multiple singleton objects(You can't release these singleton objects, when a memory warning raises).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm starting an app in iOS which will use a SQLite database.
I would like to reuse the code of this app for others in future.
I would like to make first a view associate to a ViewModel.
I will use a repository for my data. First, it will be generate, and after, data from SQLite database.
Then, when I'll use the database, just change some stuff in my ViewModel and all will work good...
I don't know if you see what I mean...
Do you know some good practices to do this? Tutorials, explanation, or anything else interesting?
EDIT:
I would like to follow this way:
Repository can be database data or random generated data for testing before I have the database...
ViewModel is the "model of myt view" which set all objects of my view with the data fetched in my Repository...
I understand the idea, but I don't know how to proceed to do this and I found nothing about the method... Maybe it's not a good way ?
If you have any suggestion, please let me know ;) Thank you!
Make an object class for the interaction with the sqlite, Preferred will be a singleton class which could also be used as intermediary storing class.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the best practice for having globally (e.g. from all view controllers) accessible current User object?
I am using core Data.
EDIT:
I just realized I should elaborate a bit (and immediately received a comment regarding that)..
current User means the user who is the owner of the phone and is associated with an entry in the remote database. User enters login/password (or registers) and then can access the remote server, get data etc..
Object visibility is a major part of app architecture. It is up to you to make your objects visible to the other objects that may need a reference to it. No simple rule can be given. I have tips in my book on how to make objects visible in an absolutely global way:
http://www.apeth.com/iOSBook/ch13.html#_global_visibility
For example, store a reference in the app delegate, which every other object can easily get a reference to. But there are lots of other solutions and approaches (which that chapter also talks about). It's a matter of planning ahead.