NSURLConnection代表 [英] NSURLConnection delegate

查看:64
本文介绍了NSURLConnection代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已修订...

应用程序的关键是与数据库服务器通信。从服务器到应用程序的响应都是XML格式的。有几个屏幕。例如,屏幕1列出了用户的信息,屏幕2列出了用户过去的交易,允许新的交易等等。

The crux of the app is communicating with a database server. Responses from the server to the app are all in XML. There are several screens. Example, screen 1 lists the user's information, screen 2 lists the user's past trades, allows new trades, and so on.

以下是我AppDelegate的一些代码:

Here is some code from my AppDelegate:

StartViewController *svc = [[StartViewController alloc] init];
TradeViewController *tvc = [[TradeViewController alloc] init];
CashViewController *cvc = [[CashViewController alloc] init];
ComViewController *covc = [[ComViewController alloc] init];
PrefsViewController *pvc = [[PrefsViewController alloc] init];

NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:5];
UITabBarController *tabBarController = [[UITabBarController alloc] init];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:svc];
[tabBarViewControllers addObject:navigationController];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:tvc];
[tabBarViewControllers addObject:navigationController];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:cvc];
[tabBarViewControllers addObject:navigationController];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:covc];
[tabBarViewControllers addObject:navigationController];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:pvc];
[tabBarViewControllers addObject:navigationController];
navigationController = nil;

[tabBarController setViewControllers:tabBarViewControllers];

[[self window] setRootViewController:tabBarController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

尝试坚持MVC风格,我有一个单独的类,可以完成所有的处理 。

Trying to stick with the MVC style, I have a singleton class which does all of the "processing".

现在举例说明我如何碰壁...用户可以在屏幕上更改他们的电子邮件地址5.在文本字段中输入新的电子邮件地址,然后单击保存按钮。然后该按钮从singleton类调用一个方法,该方法将新的电子邮件地址发送到服务器并(通过URL)并接收确认更改的XML响应。

Now an example on how I run into a wall… the user can change their email address on screen 5. Enter new email address into text field and click the save button. The button then calls a method from the singleton class which sends the new email address to the server and (via the URL) and receives a XML response confirming the change.

这里是我的问题:
1.我在进行单例类方法调用之前从视图控制器启动微调器 - 但不知道服务器发送/接收的应用程序何时完成,如何让微调器停在合适的时间?我不能从单身课那里得到它,我试过了。据我所知,它必须来自VC内部,或者有没有办法从我的单例类改变VC输出?

Here are my problems: 1. I start the spinner from the view controller before I make the singleton class method call - but not knowing when the app to server send/receive is finished, how do I make the spinner stop at the right time? I can't of it from the singleton class, I tried that. From what I know, it has to be from within the VC or is there a way to change VC output from my singleton class?


  1. 单例类NSURLConnection正在处理我的所有通信。从简单的电子邮件一直到更新事务表的所有内容。这对我来说似乎是错误的,并且很难跟踪谁在调用什么。再次,我将通过我对MVC的解释。我认为为每个VC创建一个NSURLConnection并在这些类中进行一些处理要容易得多。然而,这不会是MVC(ish)。

  1. The singleton class NSURLConnection is handling ALL of my communication. Everything from a simple, email change all the way to updating transaction tables. This just seems wrong to me and makes it very difficult to keep track on who is calling what. Again, I am going by my interpretation of MVC. I think it would be much easier to have a NSURLConnection for every VC and do some processing in those classes. However that would not be MVC(ish).

我在我的单例类中接近100个变量,数组等...我用来分配值我所有的VC。这对我来说似乎也不对,但我想不出任何其他方式。

I have close to a 100 variables, arrays, etc… in my singleton class which I use to assign values to all my VC. This also seems wrong to me but I can't think of any other way.


推荐答案


如何区分NSURLConnection委托
(connectionDidFinishLoading)正在进行哪个URL调用?

how can I distinguish in the NSURLConnection delegate (connectionDidFinishLoading) which URL call is being made?

每个委托方法(例如 -connectionDidFinishLoading:)都有一个连接参数告诉您发送消息的连接。给定的连接一次只能加载一个URL,因此URL和连接之间存在一对一的对应关系。

Each of the delegate methods (such as -connectionDidFinishLoading:) has a connection parameter that tells you which connection sent the message. A given connection can only load one URL at a time, so there's a one to one correspondence between URLs and connections.


我怎么知道下载完成后在connectionDidFinishLoading之外?

How can I tell outside of "connectionDidFinishLoading" when the download is completed?

该方法在连接完成时告诉你。您可以将该信息存储在对您的应用有用的地方。

That method tells you when the connection is finished. It's up to you to store that information somewhere where it's useful to your app.

更新:根据您添加的内容,您的处理类是你的应用程序的模型。应用程序的其余部分不应该关心每个事务都涉及到服务器的消息 - 这就是模型的业务。此外,模型不必是单个对象(更不用说单个对象) - 它可以是一组协同工作的对象。

Update: Based on what you've added, your "processing" class is your app's model. The rest of the app shouldn't care that each transaction involves a message to the server -- that's the model's business alone. Also, there's no reason that the model has to be a single object (let alone a singleton) -- it can be a group of objects that work together.

所以,你可能有一个类(我们称之为处理器),它代表应用程序与模型的接口(有些甚至可能称之为模型控制器)。 Processor的实例可能会创建一个本地数据库来存储app的当前本地状态。您可能还有一个Transaction类,它表示与服务器的单个事务。事务可以创建请求,将其发送到服务器,获取响应,更新数据库,并告知处理器事务已完成。或者,也许当应用程序的某个其他部分(如您的一个视图控制器)要求处理器处理新事务时,处理器将请求对象传递给它创建的事务,以便事务可以直接更新请求程序。

So, you might have a class (let's call it Processor) that represents the application's interface to the model (some might even call this a "model controller"). An instance of Processor might create a local database for storing the current local state of the app.You might also have a Transaction class that represents a single transaction with the server. A transaction could create a request, send it to the server, get the response, update the database, and tell the Processor that the transaction is done. Or, maybe when some other part of the app (like one of your view controllers) asks the Processor to process a new transaction, the Processor passes the requesting object along to the transaction that it creates so that the transaction can update the requestor directly.

很难说你的应用程序的最佳计划是什么,而不知道你计划在哪里采取它,但通常的指导方针是:

It's hard to say what the best plan for your app is without knowing where you're planning on taking it, but the usual guidelines hold:


  • 将问题分解为更容易解决的部分

  • break your problem into parts that are easier to solve

限制范围每个班级的职责

limit the scope of each class's responsibilities

如果事情似乎很复杂,可能是

if something seems to complicated, it probably is

将模型分成几个类也可以更容易地进行测试。您可以想象为Transaction类编写一组单元测试是多么容易。处理器也是如此 - 如果服务器事务处理器处于不同的类中,则更容易测试处理器正在做正确的事情。

Breaking your model up into several classes will make it easier to test, as well. You can imagine how easy it would be to write a set of unit tests for the Transaction class. The same goes for Processor -- if the server transaction stuff is in a different class, it's easier to test that the Processor is doing the right thing.

这篇关于NSURLConnection代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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