gpt4 book ai didi

objective-c - NSURLConnection 代表

转载 作者:行者123 更新时间:2023-11-28 18:13:30 27 4
gpt4 key购买 nike

修订...

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

这是我的 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 风格,我有一个单例类来完成所有“处理”。

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

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

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

  2. 我的单例类中有将近 100 个变量、数组等……我用它们为我的所有 VC 赋值。这对我来说似乎也是错误的,但我想不出任何其他方式。

最佳答案

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

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

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

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

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

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

如果不知道您计划将应用带到哪里,很难说什么是您的应用的最佳计划,但通常的指导原则是:

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

  • 限制每个类的职责范围

  • 如果事情看起来很复杂,那可能是

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

关于objective-c - NSURLConnection 代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10886237/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com