Skip to content

Commit

Permalink
[LEIP-295] Use Singleton to instantiate DataStore
Browse files Browse the repository at this point in the history
[LEIP-295] Use Singleton to instantiate DataStore
  • Loading branch information
hb0 authored Jan 14, 2025
2 parents 6ef5e2a + cfeb457 commit fc1c11d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions utils/src/main/kotlin/de/cyface/utils/settings/AppSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,29 @@ import java.io.File
* If this changes, consider using the standard Android Architecture, see `MeasurementRepository`.
*
* @author Armin Schnabel
* @version 2.1.0
* @version 3.0.0
* @since 3.4.0
* @param context The context to access the preferences from.
*/
@Suppress("unused") // Part of the API
class AppSettings(context: Context) {
class AppSettings private constructor(context: Context) {

/**
* Use Singleton to ensure only one instance per process is created. [LEIP-294]
*
* It should be okay to use a Singleton as this is also suggested in the documentation:
* https://developer.android.com/topic/libraries/architecture/datastore#multiprocess
*/
companion object {
@Volatile
private var instance: AppSettings? = null

fun getInstance(context: Context): AppSettings {
return instance ?: synchronized(this) {
instance ?: AppSettings(context.applicationContext).also { instance = it }
}
}
}

/**
* This avoids leaking the context when this object outlives the Activity of Fragment.
Expand Down

0 comments on commit fc1c11d

Please sign in to comment.