1

I have an AuthService singleton class that manages all Auth services (Facebook, Firebase login, etc.).

Currently I am using UserDefaults to store core current logged-in user properties as per below to use through the app. I also have the requirement track other properties such as email, name, birthday, etc.

What/How is the best practice approach to managing a current logged-in user, is it UserDefaults, Singleton User class, etc?

let defaults = UserDefaults.standard

var isLoggedIn: Bool {
    get { return defaults.bool(forKey: LOGGED_IN_KEY) }
    set { defaults.set(newValue, forKey: LOGGED_IN_KEY) }
}

var userName: String {
    get { return defaults.value(forKey: USER_NAME)  as! String }
    set { defaults.setValue(newValue, forKey: USER_NAME) }
}

var profileImgURL: String {
    get { return defaults.value(forKey: PROFILE_PIC_URL)  as! String }
    set { defaults.setValue(newValue, forKey: PROFILE_PIC_URL) }
}
A O
  • 5,516
  • 3
  • 33
  • 68
Roggie
  • 1,157
  • 3
  • 16
  • 40
  • 1
    you can make use of a struct to get its properties and save The entire struct in Userdefaults instead saving each Property Differently : Refer to : https://stackoverflow.com/a/44879409/6080920 – iOS Geek Jun 06 '18 at 04:27
  • thank you, this sure helps. – Roggie Jun 06 '18 at 06:59

1 Answers1

0

Using User defaults and Singleton Class is not mutually exclusive. The point is how to save the state of user's logged in.

First how to handle the flag LOGGED_IN_KEY?

If you put LOGGED_IN_KEY under your user class, then when saving whole User object, you need to add coder and decoder method when init.

If you set LOGGED_IN_KEY as a global variable, then just a easy User defaults set value.

From design perspective, both are reasonable. As an app with user log in functionality, the user log in status can also be deemed as an app based environment variable. However if your app, which normal apps do, allow only one user to log in at the same time, then putting the flag LOGGED_IN_KEY under user class should make the user class singleton.

User defaults is the most common way, and easiest. When the app is deleted, The User defaults saved values will also be deleted. When user reinstall the app, it starts out a new app and do not have any data about LOGGED_IN_KEY, and then ask for log in, which is an expected scenario.

Other alternative include using CoreData, but this is more like a database and a lot more set up to do. Of course you can send the flag LOGGED_IN_KEY up to your remote server and fetch it whenever the user open the app, but this requires making http request and create time delay.

So for the fastest solution should be saving LOGGED_IN_KEY separately as it only requires one key-value pair access instead of accessing whole User object.

Edison Lo
  • 476
  • 8
  • 25
  • 1
    thanks for your feedback, my question was more about handling current logged-in user properties rather than just tracking if user is logged in or not. – Roggie Jun 06 '18 at 07:00