iPhone - viewController和网络请求的体系结构

时间:2022-05-26 11:20:47

So, I have 2 types of data, some needs to be persisted and some doesn't.

所以,我有两种类型的数据,一些需要持久化,一些不需要。

I'm thinking about where to put all my network related code, inside my UIViewControllers, where all the network request start from, or in a another layer.

我正在考虑将所有网络相关代码放在我的UIViewControllers中,所有网络请求从哪里开始,或者在另一层中。

What I had in mind is this:

我的想法是这样的:

Have a layer called NetworkManager. NetworkManager is singerlton to all my web service calls.

有一个名为NetworkManager的图层。 NetworkManager是我所有网络服务电话的歌手。

For data that needs to be persistent and can be presented in a list, I would have network manager issues the request, save the response in my local core data DB, and have my UIViewController listen to that data using FetchResultsController.

对于需要持久且可以在列表中显示的数据,我会让网络管理员发出请求,将响应保存在我的本地核心数据DB中,并让我的UIViewController使用FetchResultsController监听该数据。

But, there's many other types of requests. For example : login request, user info request, friendsNearBy, and so on… some don't have to be persistent in my db, and some don't fit the FRC architecture.

但是,还有许多其他类型的请求。例如:登录请求,用户信息请求,friendsNearBy等等...有些不必在我的数据库中持久化,有些不适合FRC架构。

For these type of request, as far as I see, there are 2 ways of handling it:

对于这些类型的请求,据我所知,有两种处理方式:

1. Have another layer that separates between the ViewControllers and the NetworkManager. Let's call it Mediator. The Mediator gets the dictionary(JSON) request from the networkManager, decides according to the app logic if there's anything else needs to be done with it, and then post a notification with appropriate name and data. If the mediator saves the UIViewController who issued the request, it can delegate the response directly to him instead of posting a notification.

1.使用另一个层来分隔ViewControllers和NetworkManager。我们称之为Mediator。 Mediator从networkManager获取字典(JSON)请求,根据应用程序逻辑决定是否还需要执行任何其他操作,然后发布具有适当名称和数据的通知。如果中介保存发出请求的UIViewController,它可以直接将响应委派给他而不是发布通知。

The Flow would be like this:

流程将是这样的:

MyUiViewController - > Mediator -> NetworkManger->Mediator-> PostNotification (or directly back to MyUiViewController)

Pros:
Decoupling
Nice structure and separation of concerns

Cons:
Harder to code
Sometimes harder to understand and debug.

2. Not having this 3 layered architecture, but instead having MyUiViewControllers, issue a network request with blocks. Meaning instead of the Mediator intercepting the response before MyUiViewController, just let MyUiViewController handle the response using blocks as he is the one that issues it.

2.没有这种3层架构,而是拥有MyUiViewControllers,用块发出网络请求。意思是代替Mediator拦截MyUiViewController之前的响应,让MyUiViewController使用块处理响应,因为他是发布它的人。

Pros:
Simple and quick to code
Easy to understand

Cons:
Coupling of network code inside your controllers

I was hoping to get suggestions and comments about what's best from people's experience, or other/better way of doing this.

我希望从人们的经验或其他/更好的方式获得最佳建议和意见。

2 个解决方案

#1


1  

Have you got whats the best method already?

你有什么最好的方法吗?

Here's what i do generally,

这是我一般的做法,

Have a NetworkManager which is not Singleton. Define a protocol with method OnSuccess,OnError. Implement this in your ViewController which initiates the network connection. Set the delegate on NetworkManager and let delegate be called when Asynchronous request is executed.

有一个不是Singleton的NetworkManager。使用方法OnSuccess,OnError定义协议。在ViewController中实现它,启动网络连接。在NetworkManager上设置委托,并在执行异步请求时调用委托。

Use delegates instead of blocks as its easy to maintain.

使用委托代替块,因为它易于维护。

This may not be best solution, but hopefully it gives you some pointers.

这可能不是最佳解决方案,但希望它能为您提供一些指导。

#2


1  

I recommend option 2 with a little bit of what you listed for option 1. In my apps I tend to have two distinct modes of operation that operate concurrently.

我推荐选项2,其中包含您为选项1列出的一些内容。在我的应用程序中,我倾向于有两种不同的操作模式,它们可以同时运行。

Automatic downloads: App essential data is downloaded and saved directly to the database. It's initiated each time the app becomes active. As each request completes an NSNotification is sent out for any visible view controllers that may need to know about the new data.

自动下载:下载应用程序基本数据并直接保存到数据库。每次应用程序变为活动状态时都会启动它。当每个请求完成时,将针对可能需要了解新数据的任何可见视图控制器发送NSNotification。

For example, if I save player data I'll send a notification like "PlayerDataUpdated". When a view controller is visible it listens for notifications. When it's not visible it doesn't listen for notifications since any changes in to the database will be discovered during viewWillAppear.

例如,如果我保存播放器数据,我将发送“PlayerDataUpdated”之类的通知。当视图控制器可见时,它会侦听通知。当它不可见时,它不会监听通知,因为在viewWillAppear期间将发现对数据库的任何更改。

User Initiated downloads: For user-initiated network requests, such as pull to refresh, you should call the appropriate method on NetworkManager from the view controller that needs the updated data.

用户启动的下载:对于用户启动的网络请求,例如拉到刷新,您应该从需要更新数据的视图控制器上调用NetworkManager上的相应方法。

#1


1  

Have you got whats the best method already?

你有什么最好的方法吗?

Here's what i do generally,

这是我一般的做法,

Have a NetworkManager which is not Singleton. Define a protocol with method OnSuccess,OnError. Implement this in your ViewController which initiates the network connection. Set the delegate on NetworkManager and let delegate be called when Asynchronous request is executed.

有一个不是Singleton的NetworkManager。使用方法OnSuccess,OnError定义协议。在ViewController中实现它,启动网络连接。在NetworkManager上设置委托,并在执行异步请求时调用委托。

Use delegates instead of blocks as its easy to maintain.

使用委托代替块,因为它易于维护。

This may not be best solution, but hopefully it gives you some pointers.

这可能不是最佳解决方案,但希望它能为您提供一些指导。

#2


1  

I recommend option 2 with a little bit of what you listed for option 1. In my apps I tend to have two distinct modes of operation that operate concurrently.

我推荐选项2,其中包含您为选项1列出的一些内容。在我的应用程序中,我倾向于有两种不同的操作模式,它们可以同时运行。

Automatic downloads: App essential data is downloaded and saved directly to the database. It's initiated each time the app becomes active. As each request completes an NSNotification is sent out for any visible view controllers that may need to know about the new data.

自动下载:下载应用程序基本数据并直接保存到数据库。每次应用程序变为活动状态时都会启动它。当每个请求完成时,将针对可能需要了解新数据的任何可见视图控制器发送NSNotification。

For example, if I save player data I'll send a notification like "PlayerDataUpdated". When a view controller is visible it listens for notifications. When it's not visible it doesn't listen for notifications since any changes in to the database will be discovered during viewWillAppear.

例如,如果我保存播放器数据,我将发送“PlayerDataUpdated”之类的通知。当视图控制器可见时,它会侦听通知。当它不可见时,它不会监听通知,因为在viewWillAppear期间将发现对数据库的任何更改。

User Initiated downloads: For user-initiated network requests, such as pull to refresh, you should call the appropriate method on NetworkManager from the view controller that needs the updated data.

用户启动的下载:对于用户启动的网络请求,例如拉到刷新,您应该从需要更新数据的视图控制器上调用NetworkManager上的相应方法。