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

查看:25
本文介绍了我应该如何构建我的 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 的方法来执行站点的功能(以获取最新内容、发布内容、投票、账号管理为例).

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) 这样的方法更新,它将充当调用负责更新模型的类的包装器.

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 格式存储它们 - 只是一个简单的缓存.这至少可能是 Twitter 应用程序的方式.这样会变得更容易,但对于更大的数据集,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天全站免登陆