简体   繁体   English

iPhone + Web服务最佳实践

[英]iPhone + Web Services best practices

Accessing Web Services inside an iPhone app is a matter for which I did not find a clear, beautiful solution yet. 在iPhone应用程序内访问Web服务是我尚未找到清晰,美观的解决方案的问题。 I'm not talking about how to send queries or parse responses here, but about a "big picture" answer. 我不是在这里谈论如何发送查询或解析响应,而是在谈论“大局”。

Disregarding the server-side technology, how do/would you plug your Model objects to your Web Service ? 不管服务器端技术如何,如何/将模型对象插入Web服务? How do you design your proxy objects ? 您如何设计代理对象? How do you cache your resources ? 您如何缓存资源?

If your web service happens to be a Ruby on Rails application, then Objective Resource is a great tool: http://iphoneonrails.com/ . 如果您的Web服务恰好是Ruby on Rails应用程序,那么Objective Resource是一个很好的工具: http : //iphoneonrails.com/

If not, then what I tend to do is use ASIHTTPRequest ( http://allseeing-i.com/ASIHTTPRequest/ ) which provides a nice network layer. 如果没有,那么我倾向于使用ASIHTTPRequest( http://allseeing-i.com/ASIHTTPRequest/ ),它提供了一个不错的网络层。 Depending on the API, you might use ASI objects directly, or else you can subclass an existing ASI class if you want to add per-request functionality (such as authentication or response parsing). 根据API的不同,您可以直接使用ASI对象,或者如果要添加按请求的功能(例如身份验证或响应解析),则可以对现有的ASI类进行子类化。

You usually want to run requests in the background, so that the UI isn't blocked while waiting for the request to finish. 您通常希望在后台运行请求,以便在等待请求完成时不会阻止UI。 You can always go the background thread route, but a nice "Objective C" style approach that ASIHTTPRequest provides is to instead provide a delegate which is called when the request finishes (see also "Creating an asynchronous request" at http://allseeing-i.com/ASIHTTPRequest/How-to-use ). 您总是可以使用后台线程路由,但是ASIHTTPRequest提供的一种不错的“ Objective C”风格的方法是提供一个委托,该委托在请求完成时被调用(另请参见“创建异步请求”, 网址http:// allseeing- i.com/ASIHTTPRequest/使用方法 )。 In many cases the request delegate is the view controller that initiates the request. 在许多情况下,请求委托是启动请求的视图控制器。

The model layer depends on the complexity, and also what format the data comes in. Most of the APIs I've worked with use JSON, for which you can use SBJSON or yajl-objc to parse. 模型层取决于复杂性以及数据的格式。我使用的大多数API都使用JSON,您可以使用SBJSON或yajl-objc对其进行解析。 These usually give you the data parsed into base classes like NSString, NSArray and NSDictionary. 这些通常使您可以将数据解析为NSString,NSArray和NSDictionary等基类。 Sometimes that's sufficient, or if you want your models to exist as their own classes, then you can have the models inherit from a base class that takes care of turning the NSDictionary/NSArray into properties. 有时这足够了,或者如果您希望模型作为自己的类存在,则可以让模型从基类继承,该基类负责将NSDictionary / NSArray转换为属性。

Finally for caching, Core Data provides a good way to persist to disk. 最后,对于缓存,Core Data提供了一种持久保存到磁盘的好方法。 For caching in memory, you can have the requests occur in a separate "manager" class that is shared between controllers. 为了在内存中进行缓存,可以使请求发生在控制器之间共享的单独的“ manager”类中。 These managers use the Singleton design pattern as described here: What should my Objective-C singleton look like? 这些经理使用如下所述的Singleton设计模式: 我的Objective-C Singleton应该是什么样? .

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

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