Manage a global object or list

I have read a lot of blogs about how singletones are vulnerable in android.so my question is how to maintain such globals or a list in application.i know that general preference is one way, but is there a way to support such objects or the list is effectively any help would be more helpful.

0


source to share


9 replies


You can use a SQLite file or database to save data in Android app. You can check the links below to learn more about saving data to file or SQLite database:

Saving data to a file is ideal for storing long sequences of data that are usually read in the order https://developer.android.com/training/basics/data-storage/files.html



Storing data in a database is ideal for repetitive or structured data: https://developer.android.com/training/basics/data-storage/databases.html

+1


source


use sharedPreferences, Sqlite database to manage your objects, singletons are not very good, but static variables are harder to maintain and will check seating harder, you can use shared preferences to maintain global state if the data is not very large, if there is a large amount of data, it is recommended to use sqlite.



General preferences are extremely easy to use if you have problem using sqlite, although you can use Android orm libraries. here's a link to one: http://greenrobot.org/greendao/

+1


source


If you just want to keep the Global list until your application is running, create a new class to say Helper and Initialize a static list in this class. Now you can access this list anywhere in the application using "Helper.yourStaticListName" and you can also add / remove or get data from the list anywhere in the application. But if you want to keep this list even when the application was closed, there are two solutions for that. First, create a local database "SQLite file" in your application and add / remove or retrieve data from it.

Check out this tutorial: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

The second solution is to convert your data to JSON and convert that JSON to String and store it in general settings. And whenever you need it, just get the string from shared settings and convert it to JSON and parse to get the data. One last thing when you talk about parsing JSON then the "GSON library" is a good thing to work with.

Here is the link: http://guides.codepath.com/android/leveraging-the-gson-library

Hope this answer helps you.

0


source


How about using the Android Service ?

You can initialize / start it when your app starts (as well as stop them when your app stopped) and then bind them when you need to (put and get your object / list).

I believe this will be an efficient way.

0


source


From a conceptual point, having static variables or service locators is very similar to having singletons. Hence, using them as alternatives may be incorrect if the intent is to avoid global state and consequences.

We can change Singleton classes to instances that are instantiated only once and injected into components and methods as needed. We can use the IoC framework to handle the injection part or do it manually with a factory pattern to build (we can only restrict instance instances) to class instances. This discussion thread provides a lot of information about the problem and the different options.

0


source


So, if I understand your question correctly, you need to store some global variables throughout your application, if so please take a look at this question

basically you create a class that extends the application that will store whatever you would like when the application starts up and all of them can be accessed through the application.

hope this helps.

0


source


If you are trying to create a globally accessible object, the first thing you should ask yourself is: Why? Why do you need a globally accessible object? Most of the time you don't, and you can get away with creating a scoped object that goes around the application.

There are times when you want to access globally accessible resources and use a singleton is just one way to achieve this. According to Android Docs, your storage options are:

General settings

Store private primitive data in key-value pairs.

Internal storage

Store your personal data in the device memory.

External storage

Store publicly available data on shared external storage.

SQLite databases

Store structured data in a private database.

Network connection

Store data online using your own network server.

Singletons are great, but they have their own risks based on how they are implemented. Typically, developers use this template when you try to share a resource in the application, for example Loggers

, Print spoolers

etc. There are several ways to createSingletons

in Java, you can use Lazy Initialization or Static Initialization, each with their own pros / cons. As for "vulnerabilities", there are issues with whether a singleton is thread safe, who / what can access it, and so on. This is why it makes sense to try to understand the problem you are trying to solve. Personally, I don't understand what exactly you are trying to solve, so I cannot go into detail on how this might help you or hurt you. All I can say is that the greatest vulnerability is also the largest asset, which, like most globals, is available anywhere, anytime. It can also be a problem if the singleton is thread safe.

Personally, I think you need to evaluate what exactly you are trying to solve and choose the appropriate solution. Perhaps using a singleton is the right solution, perhaps it isn't. But understanding all of your options and the strength / weakness of each will be the best way to solve this problem. Unfortunately, you haven't provided enough context for your problem for me or anyone for that matter to give you a solid recommendation.

0


source


The best way to manage global objects is to not have them at all. In my experience, in many cases singles are used instead of alternatives. This post says so well

0


source


general priority is good, but you will feel a problem for a while when you do some modification, make a static constant variable in one java pojo class and use that variable anywhere. Because the shared preference won't change the value after use, or unless you change the preference .shared, retrieving and storing is not very unfriendly. if you are using a constant you can modify easily. Only one class you have to push

0


source







All Articles