我应该在哪里将Core Location方法放在VIPER / MVP iOS项目中?

时间:2023-01-13 23:06:36

I searched a lot, I understand that the view controller is actually the "View" in the Viper or MVP architecture and the Presenter contains the logic... But since the presenter should be easy testable, is it good to put Core Location stuff (like LocationManager and location updates) into the presenter? Or should I put in view controller and notify the presenter?

我搜索了很多,我知道视图控制器实际上是Viper或MVP架构中的“视图”,而Presenter包含逻辑......但由于演示者应该易于测试,所以放置Core Location的东西是好的(喜欢LocationManager和位置更新)到演示者?或者我应该放入视图控制器并通知演示者?

1 个解决方案

#1


2  

I'd solve this by creating a LocationService object which wraps all of the Core Location methods, and exposes their output, maybe exposing the results of location updates as a stream (if you're using Rx) or else a callback.

我将通过创建一个包装所有Core Location方法的LocationService对象来解决这个问题,并公开它们的输出,可能会将位置更新的结果公开为流(如果您使用的是Rx)或者回调。

I'd use Dependency Injection (perhaps using Swinject) to inject this into the presenter layer in MVP, or the interactor layer in VIPER (because the location information presumably affects the core business logic of the app). In order to test the app, create a 'mock' version of this service serving test data and inject it into the presenter/interactor in your tests.

我使用依赖注入(可能使用Swinject)将其注入MVP中的presenter层,或VIPER中的交互器层(因为位置信息可能会影响应用程序的核心业务逻辑)。为了测试应用程序,创建服务测试数据的此服务的“模拟”版本,并将其注入测试中的演示者/交互器。

#1


2  

I'd solve this by creating a LocationService object which wraps all of the Core Location methods, and exposes their output, maybe exposing the results of location updates as a stream (if you're using Rx) or else a callback.

我将通过创建一个包装所有Core Location方法的LocationService对象来解决这个问题,并公开它们的输出,可能会将位置更新的结果公开为流(如果您使用的是Rx)或者回调。

I'd use Dependency Injection (perhaps using Swinject) to inject this into the presenter layer in MVP, or the interactor layer in VIPER (because the location information presumably affects the core business logic of the app). In order to test the app, create a 'mock' version of this service serving test data and inject it into the presenter/interactor in your tests.

我使用依赖注入(可能使用Swinject)将其注入MVP中的presenter层,或VIPER中的交互器层(因为位置信息可能会影响应用程序的核心业务逻辑)。为了测试应用程序,创建服务测试数据的此服务的“模拟”版本,并将其注入测试中的演示者/交互器。