Introduction to DataStore - MAD Skills

Поделиться
HTML-код
  • Опубликовано: 27 ноя 2024

Комментарии • 39

  • @tenseikenzx-3559
    @tenseikenzx-3559 2 года назад +17

    Perfect timing, I'm just at the point where I need to implement shared preferences in my app. Time to use data store instead!

  • @idleidle3448
    @idleidle3448 2 года назад +14

    Nice vid. It was really clear and concise. Time to start using proto datastore! Looking forward to the other videos in the playlist

  • @ViktorYakunin
    @ViktorYakunin 2 года назад +9

    will migrate to DS only if it will have SecuredSharedPreferences like implementation out of the box.

  • @purpledaysable
    @purpledaysable 2 года назад +3

    Okay. Great to learn here. I will watch again twice more since I'm new. Thanks for sharing your knowledge.

  • @saransankaran
    @saransankaran 2 года назад +10

    What is still missing in DataStore is Encryption and support for multi process. Do we plan on add them, in the future version?

    • @ZoranSmilevski
      @ZoranSmilevski 2 года назад

      And you can't just easily read value from prefs. This is the biggest issue for me. Encryption would be also nice.

    • @anomissim
      @anomissim 2 года назад +1

      ​@@ZoranSmilevski there's also Proto DataStore, if you don't find Prefs straightforward. This will be covered in the following episodes :)

    • @ZoranSmilevski
      @ZoranSmilevski 2 года назад +1

      @@anomissim Looking forward to :)

    • @ZoranSmilevski
      @ZoranSmilevski 2 года назад

      It's here for anyone else looking: ruclips.net/video/S10ci36lBJ4/видео.html

  • @TheSentinel909
    @TheSentinel909 2 года назад

    Time to share this - super useful, palatable yet content rich.

  • @gmk57
    @gmk57 2 года назад +2

    The whole idea of having async-only API for app preferences seems very strange to me. Async APIs are great for getting info from network or large datasets from DB, but not for this use case. Most of the time we need to check preference value at a specific point of code execution, not to subscribe to any future changes. And synchronous access is crucial in many cases: to prevent race conditions in business logic, to set app theme in Activity.onCreate(), etc.
    Instead of providing out-of-the box user-friendly API like StateFlow (which supports synchronous reads), you explicitly discourage `dataStore.data.stateIn()`, on a pretext that it "would invalidate DataStore's guarantee of data consistency". What does that mean really? If `data` itself is consistent, and resulting StateFlow is a single source of truth for the app, where does inconsistency come from?
    Another major pain point: DataStore requires A LOT of boilerplate. 120+ lines of code in UserPreferencesRepository for two preferences is just ridiculous. Adding new preference would require changing code in at least four different places, not to mention launching more coroutines for reads and writes at call sites.
    There are alternative approaches, which in my opinion are easier to use and better-scaling:
    1. Good old Shared Preferences plus Kotlin delegates (write your own or use ready-made ones, e.g. github.com/NaluLabs/prefs-delegates). The result is type-safe and concise: each property declaration with default value takes 1 line, reading or writing at call site takes 1 line. Great for most cases, thread-safe, but does not provide transactional guarantees (like atomic increments) or support complex/custom data types without additional work.
    2. StateFlow holding a data class, plus a coroutine that observes it in background and saves to disk as JSON, plus an initializer which reads this JSON at app startup. Needs JSON serializer (or a specialized library like github.com/erdo/persista), but many apps have it anyway. Type-safe and scalable: each property declaration with default value takes 1 line, reading at call site takes 1 line, writing typically takes 1 line. Supports complex/custom data types & subscribing to updates out-of-the-box. With new MutableStateFlow.update() it is completely (transactionally) thread-safe.
    Second approach looks similar to "DataStore with Kotlin serialization", but the latter brings additional library, complexity and ceremony without any visible benefits.

  • @dhanshyamkamath8493
    @dhanshyamkamath8493 2 года назад +1

    Nice explanation. 👌

    • @AndroidDevelopers
      @AndroidDevelopers  2 года назад +1

      Glad you enjoyed the video, Dhanshyam!
      Don't forget to check out even more MAD Skills episodes here:
      goo.gle/madskills
      😎

  • @ChannaEdirisinghe
    @ChannaEdirisinghe 2 года назад +2

    Does datastore works together with Jetpack security lib? (Encrypted shared pref)

  • @sanler2937
    @sanler2937 2 года назад

    Does anyone saw Google publishing internal spec of implementation?
    Basically, is DataStore uses in-memory key-value map (as Shared preferences) for quick access or performs IO on every value request.

  • @mark-147
    @mark-147 2 года назад +3

    How about for multiplatform?

  • @ericjosenavarrogerman8725
    @ericjosenavarrogerman8725 2 года назад +18

    They create problems that do not exist in the apis, because the truth is that none of those mentioned with shared preferences have happened to me

    • @rmdanali
      @rmdanali 2 года назад

      Exactly

    • @theren8311
      @theren8311 2 года назад +8

      Maybe your use case just doesn't bring it up, or you're at risk of these problems and just haven't noticed. I have experienced these issues, and datastore would be nice support to these issues.
      SP is also limited, especially when it comes to complex data types

    • @rizwansayyed7893
      @rizwansayyed7893 2 года назад

      @@rmdanali true

    • @rmdanali
      @rmdanali 2 года назад +2

      @@theren8311 SP by design and documentation should never be used in complex data types.

    • @theren8311
      @theren8311 2 года назад +1

      @@rmdanali Exactly the point. If you want this functionality, you'll have to use a DB (which is over kill) or get a third party dependency that's built on top of SP to support Document type storage.

  • @kyawkyawtun2408
    @kyawkyawtun2408 2 года назад

    သာယာချမ်းမြေ့ပါစေ။

  • @jansprlak110
    @jansprlak110 2 года назад +1

    Bete tester od roku 2016 oceňujem
    a Ďakujem, Prajem požehnane dni Boh žehnaj,,

  • @seguramlk
    @seguramlk 2 года назад +1

    Where's encrypted DataStore?

  • @tomislavgodek17
    @tomislavgodek17 2 года назад +1

    Is Datastore a good option for storing the JWT token or is it better to use the AccountManager class?

    • @zalnars7847
      @zalnars7847 2 года назад +1

      Accountmanager is probably better

  • @bjugdbjk
    @bjugdbjk 2 года назад

    Super cool 👍

  • @phillaysheo8
    @phillaysheo8 2 года назад +11

    What is this nonsense? Why not fix the things with Android that are actually broken, instead of reinventing stuff that has no need to change.

  • @AdrianTache
    @AdrianTache 2 года назад

    Is there a storage limit like for sharedprefs?

  • @mondoshigua
    @mondoshigua Год назад

    🇨🇴🧔🏻👍🏼🤝🏼 Saludos desde Colombia.

  • @juanjosecastillosifuentes8748
    @juanjosecastillosifuentes8748 2 года назад

    New version android studio?

  • @ranjiealag4070
    @ranjiealag4070 2 года назад

    Ok it's greatest idea.

  • @sriki12345678
    @sriki12345678 2 года назад +2

    Oh dear why do I click on your video like you are a click bait.

  • @Бахтиёр-л4б
    @Бахтиёр-л4б 2 года назад +2

    Здравствуйте красивая девушка пожалуйста перевод по русскому.с уважением бахтиёр...ёма...

  • @rajkumarwaghade2967
    @rajkumarwaghade2967 2 года назад

    Okay

  • @jackiecheng7926
    @jackiecheng7926 2 года назад

    用mmkv吧...