简体   繁体   English

iOS RESTful应用程序设计模式

[英]iOS RESTful application design pattern

It's perfectly clear that there is no such thing as a background service in iOS . 显然, iOS中 没有 后台服务 That being said, I'm looking for a standard way to perform network requests under a design pattern. 话虽如此,我正在寻找一种在设计模式下执行网络请求的标准方法。

From an Android background, Google I/O 2010 proposes the design pattern showed in the picture below. Google I / O 2010在Android背景下提出了下图所示的设计模式。

Google I / O 2010 RESTful应用程序设计模式

It basically means that you shouldn't make a network request directly from the Activity, but instead send it to a background Service (the orange rectangle) . 基本上,这意味着您不应该直接从Activity发出网络请求,而应该将其发送到后台Service(橙色矩形) The reason behind this, listed in this document , include the fact that the system might kill the Activity when the user leaves the screen . 本文档中列出了其背后的原因,其中包括当用户离开屏幕时系统可能会杀死Activity的事实。

Going back to what I said before, I'm looking for the standards or principles related to the design patterns of RESTful iOS apps, as I have noticed that an UIViewController might stop asynchronous operations when: 回到我之前所说的,我正在寻找与RESTful iOS应用程序的设计模式相关的标准或原则,因为我注意到UIViewController可能会停止异步操作:

  1. The user press the home button and the app closes (this is to be expected) 用户按下主屏幕按钮,应用程序关闭(这是可以预期的)
  2. The device changed orientation 设备方向改变

With this in mind, let's step into the scenario where we are making a file upload to a server, let's say it's a picture. 考虑到这一点,让我们进入将文件上传到服务器的场景,假设它是一张图片。 The network request might take some time until it finishes and the user might as well change the device orientation or leave the app. 网络请求可能要花一些时间才能完成,用户也可以更改设备方向或退出应用程序。 How do we address this issue? 我们如何解决这个问题?

Note: I'm aware a solution could be just to use the delegate pattern by making the view controller the delegate of an API client class, but I feel this would violate the Single Responsibility Principle 注意:我知道一种解决方案可能只是通过将视图控制器作为API客户端类的delegate pattern来使用delegate pattern ,但是我认为这将违反单一职责原则。

You should look into NSURLSession , introduced with iOS 7, that supports background uploads and downloads. 您应该查看iOS 7引入的NSURLSession ,它支持后台上传和下载。

The NSURLSession class supports background transfers while your app is suspended. 当您的应用暂停时,NSURLSession类支持后台传输。 Background transfers are provided only by sessions created using a background session configuration object (as returned by a call to backgroundSessionConfiguration:). 后台传输仅由使用后台会话配置对象创建的会话提供(由对backgroundSessionConfiguration:的调用返回)。

So, this seems, at least superficially pretty close to what you described for Android, although it only supports download and upload operations. 因此,尽管它仅支持下载和上传操作,但这似乎至少在表面上与您对Android的描述非常接近。

You can check this document , which gives you some hints at the way you can use NSURLSession . 您可以查看此文档 ,它为您提供了使用NSURLSession的方式的一些提示。 You should specifically search for download tasks and upload tasks. 您应该专门搜索下载任务和上传任务。

Here you can find a more complete code example. 在这里,您可以找到一个更完整的代码示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM