简体   繁体   English

iOS REST设计模式建议

[英]iOS REST design pattern advice

I'd like some input on whether there is a better design pattern to use for my iOS app, which uses a REST model to communicate asynchronously with a Django back end. 我想要一些关于我的iOS应用程序是否使用更好的设计模式的意见,该应用程序使用REST模型与Django后端异步通信。

The server can presently return three types of responses to requests: 服务器目前可以返回三种类型的请求响应:

  • a JSON object JSON对象
  • a server status code integer 服务器状态码整数
  • a long Django error message 长长的Django错误消息

When an action is performed in the iOS app that requires data from the server, my design pattern looks like this: 当在需要从服务器获取数据的iOS应用中执行操作时,我的设计模式如下所示:

  • An observer is added to notification center, specifying a method that can process the server response 将观察者添加到通知中心,以指定可以处理服务器响应的方法
  • The method puts together and sends a NSURLConnection 该方法放在一起并发送NSURLConnection
  • A NSURLConnection delegate method receives the response, does some interpretation to check what kind of server response it is, and then posts the appropriate notification to the notification center NSURLConnection委托方法接收响应,进行某种解释以检查它是哪种服务器响应,然后将适当的通知发布到通知中心
  • This triggers the response method to run, processing the response 这触发响应方法运行,处理响应

My issue with this pattern is that there are a large number of methods written to send and receive individual request and response types. 这种模式的问题在于,编写了大量用于发送和接收单独的请求和响应类型的方法。 For instance, if I am requesting an item list, I need to add several observers to the notification center, one to process a user list, one to process a blank user list, and one to process errors. 例如,如果我要一个项目列表,则需要向通知中心添加多个观察者,一个用于处理用户列表,一个用于处理空白用户列表,另一个用于处理错误。 Then I need to write custom methods for each one of those three to perform the appropriate actions and remove the observers, based on what kind of response the server sends. 然后,我需要根据服务器发送的响应类型,为这三种方法中的每一种编写自定义方法,以执行适当的操作并删除观察者。

Furthermore, the NSURLConnection delegate ends up being fairly complex, because I'm trying to interpret what type of a response was received (what types of items were in the list received?) without much context of what was requested, to make sure I don't call the wrong response method when a server message comes back. 此外,NSURLConnection委托最终变得相当复杂,因为我试图解释收到的响应类型(接收到的列表中的项目是什么类型?),而没有太多关于请求内容的上下文,以确保我不会服务器消息返回时,不要调用错误的响应方法。

I am fairly new to both iOS programming and to REST programming, so I may be missing something obvious. 我对iOS编程和REST编程都比较陌生,因此我可能会遗漏一些明显的东西。 Any advice or links to resources is appreciated. 任何建议或资源的链接表示赞赏。

I'd initially look at using RestKit to abstract your code away from the network comms so you can worry more about the data model and high level requests. 最初,我会考虑使用RestKit从网络通信中抽象出您的代码,以便您可以更加担心数据模型和高级请求。 Secondly, I wouldn't use notifications for this as it will likely get messy and be very hard to manage multiple simultaneous requests - delegation or block callbacks will be much better for this. 其次,我不会为此使用通知,因为它可能会变得凌乱,并且很难同时管理多个请求-为此,委托或阻止回调会更好。

Your REST implementation is mostly server side, and emprirically you'd be passing and receiving binary. 您的REST实现主要是在服务器端,并且通常情况下,您将传递和接收二进制文件。 There are factors to consider, including whether you are utilizing HTTP. 有许多因素需要考虑,包括您是否正在使用HTTP。

Working with JSON with NSJSONSerialization class, and NSURLConnection keeps your program more lean and mean. 使用NSJSONSerialization类和JSON与NSURLConnection一起使用JSON可使您的程序更加精简和NSJSONSerialization

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

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