如何使用Koin注入数据类?
当我的应用程序启动时,我会用一些数据创建对象,我想在服务/ViewModels之间共享对象的相同实例。
是否可以使用KOIN将数据类的同一实例注入观看模型?
编辑: 我在mainViewModel
中创建用户对象,当时从Firebase加载数据时。
@IgnoreExtraProperties
@Keep
data class User(
val id: String = "",
val name: String? = null,
val surname: String? = null,
val email: String? = null,
val avatarUrl: String? = null
)
When my application starts I create object with some data and I want to share the same instance of object between services/viewModels.
Is it possible to inject the same instance of data class to viewModel using Koin?
Edit:
I create user object in MainViewModel
when app loaded data from firebase at start.
@IgnoreExtraProperties
@Keep
data class User(
val id: String = "",
val name: String? = null,
val surname: String? = null,
val email: String? = null,
val avatarUrl: String? = null
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的视图模型从
koIncomponent
继承,则可以访问getkoin
方法来声明您的用户对象。声明后,用户对象将用于您的其余应用程序。
希望,它有帮助。
If your view model inherits from
KoinComponent
, you can accessgetKoin
method to declare your user object.The user object will be available to rest of your application after declaration.
Hope, it helps.
我将创建一个持有器对象,例如
usermanager
,以容纳一个可选的用户
实例。您可以在koin图中以和任何组件负责设置
用户
实例(例如您的mainViewModel
),您可以在Koin图中提供此支架。可以更新Singleton持有人内部的实例。I would create a holder object, say
UserManager
, to hold an optionalUser
instance. This holder is something you can provide in your koin graph assingle
, and whatever component responsible setting up theUser
instance (for example yourMainViewModel
) can update the instance inside the singleton holder.