Daniel Faria
Daniel Faria

Reputation: 1484

Local storage best approach

I'm coding a app that receive data about the current user from a Web service and store the information in a local sqlite storage to make this data avaiable when the internet connection is'nt available.

When a user logout and another one login I replace all information of the old user with the new user informationm, ie, the information in the local storage always will be of the current user, so i dont have to worry about table relationships, for instance:

The table "messages" dont have to be a field "user_id" because all the messages always will be from the current user loged in.

I'm new on mobile dev and I need to know if this aproach is a good point, and if not, what is the best practice to handle cases like that.

Upvotes: 2

Views: 97

Answers (1)

Gentatsu
Gentatsu

Reputation: 717

It's best to use shared preferences for username/passwords, in my opinion. They're an easy way to store information that can be accessed anywhere throughout the app and be changed.

As such:

To obtain shared preferences, use the following method In your activity:

SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

To read preferences:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

Obtained from: How to use SharedPreferences in Android to store, fetch and edit values

Upvotes: 1

Related Questions