iPhone - 更改委托可能会导致异步调用崩溃?

发布于 2024-11-28 03:05:05 字数 624 浏览 0 评论 0原文

我有一个类实例化另一个类并作为第二个类的代表。第二类是“免费”的,不会被第一类保留。

第二个类发送异步 HTTP 请求并侦听它们的响应。
当收到响应时,将对其进行解析,并将结果重新打包并发送到委托方法之一。一旦调用委托,第二个类实例就会释放自身。演出结束。

为了确保获得服务器答案,当第一个类进入 dealloc 时(出于任何原因),它会更改第二个类的 delegate 属性以将答案路由到 applicationdelegate。

但是......当更改该委托属性时,我认为异步的http答案有可能与第一类的dealloc过程发生冲突。因此,第一类在释放时会收到答案。在这种情况下,第一个类将收到它无法管理的答案(可能会崩溃),而第二个类永远不会看到委托在调用发送到第一个类后发生了更改。

您将如何具体解决这个问题?

以下是该过程的架构:

  • AppDelegate 创建 A1、A2、A3。
  • 每个 Ax 都被定义为 Bx 的委托
  • 每个 Ax 创建一个 B 实例对象(发送 HTTP 请求的 B1、B2、B3),此时
  • ,如果 Ax 实例死亡,所有 A 和 B 类实例都可能有生命, Bx 类可以将答案发送给 appdelegate 而不是 Ax 实例

I have a class that instanciate another class and works as a delegate for that second class. That second class is "free" and is not retained by the first one.

That second class sends async HTTP requests and listen for their response.
When a response is received, it is parsed, and the result is repackaged and sent to one of the delegate methods. Once the delegate is called, the second class instance release itself. End of the show.

To be sure to get the server answer, when the first class enters in dealloc (for any reason), it changes the delegate property of the second one to route the answer to the applicationdelegate.

But... when changing that delegate attribute, I think there is a chance that the http answer that is async may collide with the dealloc process of the first class. So the first class would receive an answer while it is deallocating. In that case, the first class would receive an answer it cannot manage (may probably crash), and the second one would never see that the delegate had changed just after the call has been sent to the first class.

How would you concretly manage that problem ?

Here is a schema of the process :

  • AppDelegate creates A1, A2, A3.
  • each Ax create a B instance object (B1, B2, B3 that send HTTP requests), and each Ax is defined as a delegate of Bx
  • at this point, all A and B class instance may have their lives
  • if a Ax instance dies, the Bx class may send the answer to the appdelegate instead of the Ax instance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫思念不要吵 2024-12-05 03:05:05

如果 AB 的代表,但不允许 A 保留 B,则会出现问题。

  1. B 必须知道委托(A) 仍然存在,或者在需要时设置为nil
  2. A 必须知道B 仍然存在,否则它无法安全地将自己作为委托删除。

因此,要么删除保留限制,然后让 A 正确保留 B

或者使用通知来寻求耦合度较低的解决方案。

  1. B 定义一个通知,并在其上发布。
  2. A 在创建时注册为通知的侦听器,并在释放时取消注册。

Your have a problem if A is a delegate for B, but A is not allowed to retain B.

  1. B must known that is delegate (A) is still around, or set to nil when needed.
  2. A must know that B is still around, or else it can not safely remove itself as delegate.

So either remove the retain restriction and let A properly retain B.

Or go for a less coupled solution using notifications.

  1. Let B define a notification, and post on it.
  2. Let A register as a listener for the notification at creation, and deregister on deallocation.
雨巷深深 2024-12-05 03:05:05

为了确保获得服务器答案,当第一个类进入 dealloc(出于任何原因)时,它会更改第二个类的委托属性以将答案路由到应用程序委托。

对我来说,这听起来不像是一个明智的架构。没有必要像你一样应付代表们。将与服务器通信的代码分解为它自己的类,只要您需要它,该类就会持续存在。不要将网络代码分散在整个应用程序中不断进出的对象中,并且当您需要在多个位置提供可用的东西时,不要将应用程序委托视为包罗万象。

To be sure to get the server answer, when the first class enters in dealloc (for any reason), it changes the delegate property of the second one to route the answer to the application delegate.

This doesn't sound like a sensible architecture to me. Juggling delegates in the way that you are shouldn't be necessary. Factor out the code that communicates to the server to its own class that persists for as long as you need it to. Don't spread your networking code throughout your app in objects that are coming and going all the time and don't treat your app delegate as a catch all whenever you need something available in more than one place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文