我应该如何构建我的 iPhone 应用程序以与我的网站对话? [英] How should I architect my iPhone app to talk to my website?

查看:26
本文介绍了我应该如何构建我的 iPhone 应用程序以与我的网站对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计划我的第一个 iPhone 应用程序,我想从一开始就了解如何构建它.正在构建 iPhone 应用程序以与已经用 PHP 构建的面向公众的 Web 应用程序配对.

I'm planning my first iPhone app and I'd like to get some inputs as to how to build it, right from the start. The iPhone app is being built to be paired with a public facing web application that is already built in PHP.

我希望网络平台成为中心(数据存储在 mySQL 数据库中),并且拥有iPhone 客户端与它交谈并使用 REST'ful 方法来执行站点的功能(例如获取最新内容、发布内容、投票、帐户管理).

I'd like the web platform to be central (data is housed in a mySQL database), and have the iPhone clients talk to it and use REST'ful methods to perform the functions of the site (fetching latest content, posting content, voting, account management as examples).

我希望客户端获取 SQLite 数据库中数据的本地副本,但刷新以获取最新版本的提要(类似于 Twitter 应用程序).

I'd like the clients to get a local copy of the data in a SQLite database, but refresh to get the latest version of the feed (similar to the Twitter app).

我现在的一些想法:

  • 使用 ASIHTTPRequest 之类的东西向服务器上的 PHP 文件发送/接收数据以侦听请求

  • Use something like ASIHTTPRequest to send/recieve data to PHP files on the server listening for requests

JSON - 我最好将 GET/POSTS 发送到返回 JSON 对象的 PHP,并使用某种管理数据并将更改传达给本地 SQLite 数据库的包装器?

JSON - would I be better off to send the GET/POSTS to a PHP that returns JSON objects, and work with some sort of wrapper that manages the data and communicates changes to the local SQLite database?

我完全不知道应该如何构建这个东西来与网络通信吗?是对此有最佳做法吗?

Am I totally off in how I should be building this thing to communicate with the web? Is there a best practice for this?

非常感谢您对如何构建这种设置的任何意见.

I'd really appreciate any input on how you would architect this sort of a setup.

谢谢,

编辑:再次阅读我自己的帖子后,我知道它听起来像一个 Twitter 客户端,但它不是,尽管它具有类似 Twitter 类型设置的功能/结构.谢谢!

EDIT: After reading my own post again, I know it sounds like a Twitter client, but it is NOT, although it has similar features/structure of a Twitter type setup. Thanks!

推荐答案

正如您在计划中已经概述的那样,XML 和 REST 是与 Web 应用程序通信的好方法.我想建议一些关于如何实际设计和构建它的细节,或者你应该记住什么.

As you already outlined in your plan, XML and REST are a great way to communicate with a web application. I want to suggest few details about how to actually design and build it, or what you should keep in mind.

首先,我认为坚持使用 MVC 很重要.我见过人们在视图控制器中创建 HTTP 连接,控制器是 NSXMLParser 的委托,控制器包含成员变量中的数据.我什至见过 UITableCells 建立 HTTP 连接.不要这样做!

First of all, I believe it's important to stick with MVC. I've seen people creating HTTP connections in view-controllers, controllers being NSXMLParser's delegate, controllers containing data in member variables. I've even seen UITableCells establishing HTTP connections. Don't do it!

您的模型及其基本操作代码应尽可能多地从用户界面中提取.由于您已经在 Web 应用程序中创建了模型,因此请尝试在您的 iPhone 项目中重新创建实体.不要害怕实体类中有一些简单的方法,但不要让它们使用外部资源,尤其是tcp连接.作为实体类中方法的示例,您可能具有以特定方式格式化数据的方法(例如日期,或将全名作为名字和姓氏的串联返回),或者您甚至可以拥有像 - (void) 这样的方法update 将充当包装器来调用负责更新模型的类.

Your model and its basic manipulation code should be as much extracted from user interface as possible. As you already have created the model in your web-application, try to recreate the entities in your iPhone project. Don't be afraid of having some simple methods in entity classes, but do not make them use external resources, especially tcp connections. As an example of methods in entity class you might have methods that formats data in specific ways (dates as an example, or returning fullname as concatenation of firstname and surname), or you can even have a method like - (void)update that would act as a wrapper to call class responsible to update the model.

创建另一个用于更新模型的类 - 从 web-app 获取 XML.甚至不考虑使用同步连接,甚至不考虑使用专用线程.与委托的异步连接是要走的路.有时需要发出多个请求来获取所有必需的数据.您可能希望创建某种状态机来保存有关您处于下载哪个阶段的信息,以及从一个阶段到另一个阶段的进展,如果发生错误则跳到最后,片刻后从失败的阶段重新执行.

Create another class for updating the model - fetching the XMLs from web-app. Do not even consider using synchronous connections, not even from a dedicated thread. Asynchronous connections with delegate is the way to go. Sometimes multiple requests need to be made to get all required data. You might want to create some kind of state-machine to keep the information about in which stage of downloading you are, and progress from stage to stage, skipping to the end if error occurs, re-executing from failed stage after some moments.

在某处临时下载数据,首先当你拥有所有数据时,进行切换并更新用户界面.这有助于在启动应用时做出响应 - 用户可以立即使用本地存储的数据,同时更新机制正在下载新数据.

Download data somewhere temporarily, and first when you have it all, make a switch and update user interface. This helps responsiveness during launching the app - user gets to work immediately with data stored locally, while the update mechanism is downloading the new data.

如果您需要下载大量文件,请尝试同时下载它们,前提是文件之间的依赖关系允许这样做.这涉及为每个请求创建一个连接,可能为每个请求委托实例.当然,所有这些连接都可以只有一个委托实例,但跟踪数据会变得有点复杂.同时下载可能会大大减少延迟,从而使该机制对用户来说更快.

If you need to download lots of files, try to download them simultaneously, if dependencies between files allow for that. This involves creating a connection per request, probably delegate instance for each of them. You can of course have only one delegate instance for all of those connections, but it gets a bit more complex to track the data. Downloading simultaneously might decrease latency considerably, making the mechanism much faster for the user.

为了节省时间和带宽,请考虑使用 HTTP 的 If-Modified-Since 和/或 ETag 标头.记住上次请求数据的时间或标签,下次在 HTTP 的标头中发送.如果内容未更改,您的 Web 应用程序应返回 HTTP 代码 304.iPhone 应用程序应在 connection:didReceiveResponse: 中相应地对此代码作出反应.

To save the time and bandwidth, consider using HTTP's If-Modified-Since and/or ETag headers. Remember the time or tag when you requested the data the last time, and next time send it in HTTP's header. Your web-application should return HTTP code 304 if content has not been changed. iPhone app should react on this code accordingly in connection:didReceiveResponse:.

创建一个专门的类来解析 XML 并更新模型.您可以使用 NSXMLParser,但如果您的文件不是很大,我强烈推荐 TouchXML,使用 XML 作为文档(它也支持 XPath)而不是基于事件的 API 是一种乐趣.您也可以在下载文件时使用此解析器来检查其有效性 - 如果解析失败,请重新下载.这时候专门的解析类就派上用场了.

Create a dedicated class to parse the XML and update the model. You can use NSXMLParser, but if your files are not huge I strongly recommend TouchXML, it's such a pleasure to work with XML as document (it also supports XPath), instead of an event based API. You can use this parser also when files are downloaded to check their validity - re-download if parsing fails. That's when dedicated class for parsing comes handy.

如果您的数据集不是很大,如果您不需要将下载的数据永久保存在 iPhone 上,您可能不需要将它们存储在 SQLite 数据库中,您可以简单地将它们存储为 XML 格式 - 只是一个简单的缓存.这至少可能是推特应用程序的方式.这样会更容易,但对于更大的数据集,XML 会消耗大量内存和处理能力——在这种情况下,SQLite 更好.

If your dataset is not huge, if you do not need to persist downloaded data on iPhone forever, you probably don't need to store them in SQLite database, you can simply store them in XML format - just a simple caching. That at least might be the way for a twitter app. It gets easier that way, but for bigger data sets XML consumes lots of memory and processing power - in that case SQLite is better.

我建议使用 Core Data,但你提到这是你的第一个 iPhone 应用程序,所以我建议你不要使用它.然而.

I'd suggest using Core Data, but you mention this is your first iPhone app, so I suggest you don't use it. Yet.

不要忘记多任务处理 - 您的应用可能会在下载过程中进入休眠状态,您需要取消连接并清理更新机制.在应用唤醒时,您可能希望恢复更新.

Do not forget about multitasking - your app can go to sleep in the middle of download, you need to cancel connections, and cleanup your update mechanisms. On app's wake-up you might want to resume the update.

关于应用程序的视图部分 - 使用 Interface Builder.一开始可能会很痛苦,但从长远来看会有所回报.

Regarding the view part of the application - use Interface Builder. It might be painful in the beginning, but it pays off in the long run.

视图控制器是模型和视图之间的粘合剂.不要在其中存储数据.三思而后行,在哪里实施什么,由谁来调用它.

View controllers are the glue between model and views. Do not store data in there. Think twice about what to implement where, and who should call it.

这与应用程序的架构无关,但我想提醒一下,Objective-C 是非常富有表现力的语言.代码应该读起来很像一个句子.使用协议扩展类.举个例子,前几天我需要一个字符串的第一行.当然,您可以编写一个单行代码,在其中找到第一次出现的换行符,并从头到尾获取一个子字符串.但它看起来不正确.我已将 - (NSString*)firstLine 添加到我的 NSString 协议中.这样代码看起来好多了,不需要任何注释.

This is not related to architecture of the app, but I want to remind that Objective-C is very expressive language. Code should read much like a sentence. Extend classes with protocols. As an example the other day I needed first line of a string. Sure, you can write a one-liner where you find first occurrence of a new-line, and get a substring from beginning till there. But it doesn't look right. I've added - (NSString*)firstLine into my NSString's protocol. Code looks so much better this way, it doesn't need any comments.

在任何项目的架构和设计中都有很多事情需要考虑,它们应该齐头并进.如果一个人给另一个人造成麻烦,你需要适应.没有什么是一成不变的.

There are lots of things to consider in both architecture and design of any project, they both should go hand in hand. If one is causing trouble to the other, you need to adapt. Nothing is written in stone.

这篇关于我应该如何构建我的 iPhone 应用程序以与我的网站对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆