简体   繁体   English

如何在Android中的MVP架构中的两位演示者之间共享数据?

[英]How to share data between two presenters in MVP architecture in Android?

Here's an example scenario: 这是一个示例场景:

I have an activity (view) and a presenter for that view. 我有一个活动(视图)和该视图的演示者。 The presenter fetches a list of users from a network API and holds it in memory using a List object. 演示者从网络API中提取用户列表,并使用List对象将其保存在内存中。 The activity contains different types of fragments to display the content about the users based on User.type. 活动包含不同类型的片段,以根据User.type显示有关用户的内容。 The two fragments (UserType1Fragment and UserType2Fragment) have their own respective presenters too. 这两个片段(UserType1Fragment和UserType2Fragment)也有各自的演示者。

The activity's presenter decides what type (I or II) of fragment is shown next based. 活动的演示者决定下一个片段的类型(I或II)。 The fragments' presenters decide how the user object is displayed and handle a button click event called killUser(). 片段的演示者决定用户对象的显示方式,并处理名为killUser()的按钮单击事件。 This should update the List object in the activity's presenter. 这应该更新活动的演示者中的List对象。

This is where the problem lies: 这就是问题所在:

How do the fragment presents have a reference to the data in activity presenter? 片段如何呈现对活动演示者中的数据的引用? The presenters shouldn't directly communicate with each other. 演示者不应直接相互沟通。 Maybe I should abstract out the List into a repository/interactor? 也许我应该将List抽象到存储库/交互器中? How would the List be shared among presenters? 如何在演示者之间共享列表?

So I ended up implementing something like what @Jahnold recommended. 所以我最终实现了@Jahnold推荐的东西。 (I'll post the diagram in the link provided for an idea stackoverflow.com/a/41966497/568898 ) (我会在为想法提供的链接中发布图表stackoverflow.com/a/41966497/568898

Hannes Dorfmann (the guy who created/manages the famous Mosby MVP library : Github link ) also pointed me in this direction. Hannes Dorfmann(创建/管理着名的Mosby MVP库的人: Github链接 )也向我指出了这个方向。

在此输入图像描述

Implementation 履行

I have a presenter for the main activity and multiple fragments that can be used in that activity. 我有一个主要活动的演示者和可以在该活动中使用的多个片段。 Each fragment has its own presenter. 每个片段都有自己的演示者。 Then I use a repository (search for repository pattern) which is basically stores the models and the business logic. 然后我使用存储库(搜索存储库模式),它基本上存储模型和业务逻辑。 For my use case, I keep this repository as a singleton. 对于我的用例,我将此存储库保留为单例。 The repository provides data in three forms, from an online api, an sqllite database, or a cache stored in memory (basically an arraylist of items). 存储库以三种形式提供数据,从在线api,sqllite数据库或存储在存储器中的缓存(基本上是项目的arraylist)。 I also have some currentitem int indexes and stuff in this repository, that get updated based on the current state. 我在这个存储库中也有一些currentitem int索引和东西,它们会根据当前状态进行更新。

Hence the data, the state and the business logic are stored in this shared repository. 因此,数据,状态和业务逻辑存储在此共享存储库中。 The presenters and the views are pretty dumb. 主持人和观点非常愚蠢。 I don't have much business logic (application specific logic) in presenters. 我在演示者中没有太多业务逻辑(特定于应用程序的逻辑)。 They simply have the logic associated with how the data has to be displayed (view specific logic) and preprocessing in logic them. 它们只是具有与如何显示数据(查看特定逻辑)和逻辑预处理相关的逻辑。

Example

Whenever the fragment and activity need to talk to each other (via presenters) when the user clicks a button in a child fragment, the fragment asks its presenter to handleClick, the presenters updates the repository's currentItemSelected data (or something else) and asks the fragment to fire an event (say onbuttonclick) to an interface listener which the activity implements. 每当片段和活动需要在用户单击子片段中的按钮时相互交谈(通过演示者),片段会要求其演示者处理ClickClick,演示者更新存储库的currentItemSelected数据(或其他内容)并询问片段将事件(例如onbuttonclick)发送到活动实现的接口侦听器。 When the activity gets the event, it asks it's own presenter to handle it and in turn the activity presenter looks for an update in the repository to get the new currentItemSelected. 当活动获取事件时,它会要求它自己的演示者处理它,然后活动演示者在存储库中查找更新以获取新的currentItemSelected。

Extra Info (advanced version) : 额外信息(高级版)

You can also follow Clean architecture which is sort of a more advanced version of MVP architecture. 您还可以关注Clean architecture,它是MVP架构的更高级版本。 MVP just deals with the architecture of the views, where as Clean architecture also deals with business logic and data architecture, MVP is just a small part of clean arch which is used to handle the views. MVP只处理视图的体系结构,其中Clean体系结构也处理业务逻辑和数据体系结构,MVP只是用于处理视图的干净拱的一小部分。 Using this, you can break down the mega repo in my case into even further use-cases (or interactors) that handle a specific business logic use-case, and the repository just provides data. 使用它,您可以将我的案例中的mega repo分解为处理特定业务逻辑用例的更多用例(或交互器),并且存储库只提供数据。 So the logic flow is now view-->presenter-->interactor-->repo and back. 所以逻辑流程现在是视图 - >演示者 - >交互器 - > repo和返回。

you can pass list reference to fragment through newInstance() of fragment. 你可以通过片段的newInstance()将列表引用传递给片段。 I think the presenters shouldn't directly communicate with each other. 我认为演示者不应该直接相互沟通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android中的MVP-如何在演示者之间交换数据/事件? - MVP in Android - How to Exchange data/events between Presenters? MVP Android - 有多少主持人? - MVP Android - How many presenters? 在MVP android应用程序中的演示者之间进行通信 - communicate between presenters in MVP android application 如何在两个片段之间共享数据? MVVM 架构有问题 - How to share data between two fragments? Having trouble with the MVVM architecture 如何使用 MVP 架构将数据添加到 android 中的 Firestore? - How to add data to Firestore in android using MVP Architecture? Android MVP - 屏幕方向 - 保留演示者状态 - Android MVP - screen orientation - retain presenters state Android MVP架构中如何分离Service和Activity? - How to separate Service and Activity in Android MVP architecture? 具有MVP的Android架构组件 - Android architecture components with MVP 使用Android Clean架构在用例之间共享域数据 - Share domain data between usecases using android clean architecture 如何在android中的两个或多个应用程序之间安全地共享数据? - How to securely share data between two or more applications in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM