kotlin库_Kotlin的存储库中不再有livedata

kotlin库

什么是LiveData? (What Is LiveData?)

We can describe LiveData as an observable data holder class, meaning it can hold a set of data that can be observed from other Android UI components such as activities, fragments, and services.

我们可以将LiveData描述为可观察的数据持有者类,这意味着它可以保存可以从其他Android UI组件(例如活动,片段和服务)观察到的一组数据。

LiveData is lifecycle-aware. Once the component that is observing data is destroyed or not active, LiveData stops publishing data to that observer. This solves many common problems for Android developers.

LiveData具有生命周期意识。 一旦用于观察数据的组件被破坏或不活动,LiveData将停止将数据发布到该观察器。 这为Android开发人员解决了许多常见问题。

Most developers use LiveData in an MVVM architecture to communicate or transmit data from the ViewModel to the view. This ensures lifecycle-aware communication between them by reducing memory leaks.

大多数开发人员在MVVM架构中使用LiveData来将数据从ViewModel传递或传输到视图。 这样可以通过减少内存泄漏来确保它们之间的生命周期感知通信。

But there are cases where I saw developers use LiveData to communicate between a repository and a ViewModel, including a Google I/O application. Using LiveData in this case seems correct. After all, it’s designed to hold and pass the data. But it’s not recommended, and you’ll learn why in the following sections.

但是在某些情况下,我看到开发人员使用LiveData在存储库和ViewModel(包括Google I / O应用程序)之间进行通信。 在这种情况下使用LiveData似乎是正确的。 毕竟,它是设计用来保存和传递数据的。 但不建议这样做,您将在以下各节中了解原因。

使用LiveData的优势: (Advantages of using LiveData:)

  • No memory leaks

    没有内存泄漏
  • No crashes due to stopped components

    没有因组件停止而导致的崩溃
  • No more manually lifecycle handling

    不再需要手动进行生命周期处理
  • Resource sharing

    资源共享

什么是Kotlin Flow? (What Is Kotlin Flow?)

Before coroutines, most developers used RxJava for asynchronous programming. When Kotlin came up with coroutines, developers loved them. They solved all the traditional problems with async programming in a more sophisticated way than RxJava.

在进行协程之前,大多数开发人员都使用RxJava进行异步编程。 当Kotlin提出协程时,开发人员很喜欢它们。 他们使用异步编程以比RxJava更复杂的方式解决了所有传统问题。

Before coroutines, we had to call an asynchronous function like a service call, retrieving data from the database, reading files, or anything via callbac

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值