Last active
August 7, 2020 00:13
-
-
Save shafty023/3257cb102b7a91a10df97f6fdb00429e to your computer and use it in GitHub Desktop.
Retrieve Storable instance from storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} | |
} |
#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
#16 should be fromJson()