Skip to content

Instantly share code, notes, and snippets.

@shafty023

shafty023/get_storable.kt

Last active Aug 7, 2020
Embed
What would you like to do?
Retrieve Storable instance from storage
/**
* Retrieves the [Storable] instance from prefs.
*
* @param context the caller's context
* @return the storable instance
*/
fun getStorable(context: Context): Storable? {
val prefs = context.getSharedPreferences("database",
Context.MODE_PRIVATE)
val serialized = prefs.getString("key", null)
if (serialized.isNullOrBlank()) {
return null
}
return try {
Gson().fromJson(serialized,
object: TypeToken<Storable>() {}.type)
} catch (ex: JsonSyntaxException) {
null
}
}
@wingsum93

This comment has been minimized.

Copy link

@wingsum93 wingsum93 commented Aug 7, 2020

#16 should be fromJson()

@shafty023

This comment has been minimized.

Copy link
Owner Author

@shafty023 shafty023 commented Aug 7, 2020

#16 should be fromJson()

Ah nice catch. Updated code snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.