0

I have an app which user can login and each user have different profiles. We are maintaining separate db, separate shared-preferences with different names for each profile. Whenever we switch profile, we get data from respective db/shared-preference.

Is there any API like maintaining a single db/shared-preference for multiple profiles - something like each profile will have a separate app data, so that we need not to create separated shared preference/db with different name for each profile. Each profile can have shared-preference/db with same name. So, each profile have its own app-data but same shared-preference/db names.

I guess accounts in android will have separate app-data, but here we need to have separate app-data for profiles.

Dileep Perla
  • 1,865
  • 7
  • 33
  • 54
  • Why do you need separate db? You can store the multiple profile data in single db or in a single table. – Aman Gupta - ΔMΔN Mar 01 '16 at 10:04
  • @AmanGupta Each profile have lot of tables and shared-preferences. Suppose if I store user_points of profile_1 in shared preference, then I need to give different name for user_points of profile_2. This goes for multiple profiles. Same with the case of tables. For the same thing, i need to give different names for different profiles manually. – Dileep Perla Mar 01 '16 at 10:11
  • Ok maybe I can't understand your whole scenario but you don't need to create separate shared preferences for every profile. You can use a **HashSet** or **JSON** in shared preferences for storing the multiple profile data. – Aman Gupta - ΔMΔN Mar 01 '16 at 10:32
  • one solution i can think is that you can assign a unique user id to your users and use that userid as a folder for all his profile related dbs. for sharedprefs you can prefix name like userid_point. even you can create some helper methods that will do this automatically like getPointPreference(userid) – Vivart Mar 01 '16 at 10:39
  • @AmanGupta my whole point is to create shared-preferences or tables with same name for multiple profiles. – Dileep Perla Mar 01 '16 at 11:38

1 Answers1

0

Sadly, there are no shortcuts here.

In your database, you will need a column for your username/account name (or however you are identifying a profile) in every table that has a profile-specific data entity. All your queries on the table will need to include the profile name in a WHERE clause to get the data for that profile.

In your preferences, you would append the profile name to the preference key to make it the preference for that profile. In my app I created a static getKey() method to take the current username and append it to a key, then return that key.

kris larson
  • 30,387
  • 5
  • 62
  • 74