0

First, I am using Kotlin not Java for this app, so I'd like solutions to be in Kotlin, please.

I am trying to figure out the best practices for loading a user's profile information from Firebase when the fragment is first loaded, but on subsequent loads, don't query Firebase and re-download the information (Data persistence?). I was initially thinking about using saveSharedPreferences, but not sure if that is the correct route to go. I was thinking I could store this info in saveSharedPrefs as well as Firebase and then query if saveSharedPrefs is null or not. But also, I'd need to check for data changes. Logically, if the user updated their profile, I'd update saveSharedPrefs as well as Firebase, so only checking for null would be necessary. I'm looking at storing username, photo, email, bio, interests, city, website and maybe 1 or 2 more items so there wouldn't be a lot of stored local data. Plus, I'd only be storing non-sensitive information.

Is this the best and most efficient way of handling data? Is there another method I am not thinking about? I'd also like to apply this to other parts of the app such that, unless the data from Firebase differs from the local data, don't reload from Firebase, get the data from saveSharedPrefs.

Please help with suggestions. Also, I've not done much with saveSharedPrefs yet, so I'm still learning how to do that. Thanks!

*****Update*****

Get photo from Firebase:

Here's my code for loading the user profile info (Just the Name and Photo uri):

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            doAsync {
                //get user profile information
                if (user != null) {
                    uiThread {
                        //update UI thread after completing task
                        view.profileUsername.setText(user?.displayName)
                        view.profileProgressBar.visibility = View.GONE
                        if (user?.photoUrl != null) {
                            view.profileUserPhoto.setImageURI(user?.photoUrl)

                        }
                    }
                }
            }
        }

set onClickListener for the floatingActionButton and the call the chooseProfileImage method to get the image from local storage:

view.fab_profile_photo_edit.setOnClickListener {

            chooseProfileImage()

        }

 private fun chooseProfileImage() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), profileImageRequest)
    }

These actions are all performed inside a fragment, just fyi.

Jeff McBride
  • 73
  • 12

1 Answers1

0

If you want your data alway update you must load data each time reload Fragment, but you think user uncomfortable with that, it's fine. I will use SQlite in Android, load data and save it in database the first time. When I click fragment i load data from database to make sure fast and alway data to for user can be read, and aloso load data from firebase, I check when load done and update data to sqlite and reaload data for view,

It's maybe like this:

private loadDataFromFirebase(){
// Here load data from firebase, when load done, call bindaDataForView(data)

}

private bindDataForView(Data data){


}
Hoàng Vũ Anh
  • 647
  • 1
  • 8
  • 24
  • I am using Firebase, not SQL. Firebase uses Nosql. It's not really SQL, but more like Json as SQL. But, you shouldn't have to load the user info each time you load the fragment unless this data changes (which isn't commonplace with a user profile. It's not a matter of user being uncomfortable, it's a matter of app speed. Or for that matter, you shouldn't have to load the data each time the user starts the app. You should only load user information if something about the info changes. That data should persist throughout the user's session unless the data changes for some reason. – Jeff McBride Sep 01 '18 at 04:29
  • SQLite to save data local, here https://developer.android.com/training/data-storage/sqlite – Hoàng Vũ Anh Sep 01 '18 at 04:50
  • If you use Adapter, you just call notifyDataSetChanged, i think this is sipmple case. read more: https://stackoverflow.com/questions/7951730/viewpager-and-fragments-whats-the-right-way-to-store-fragments-state?rq=1 – Hoàng Vũ Anh Sep 01 '18 at 04:53