开发者

NSOperation finishes in the background, attempts to notify main thread, view no longer exists. Crash

开发者 https://www.devze.com 2023-03-07 02:44 出处:网络
I have an NSOperation running not in the main thread. It is spawned from a UITableViewController. When the operation is complete, I\'d like to reload the tableview since some data h开发者_运维技巧as c

I have an NSOperation running not in the main thread. It is spawned from a UITableViewController. When the operation is complete, I'd like to reload the tableview since some data h开发者_运维技巧as changed. I've set a delegate for the background to notify on completion. When done, I call a wrapper around reloadData specifically on the main thread using performSelectorOnMainThread.

For the most part, this works well, however, there is a non-0 chance that the original (edit)tableViewController (/edit) gets released and I get zombie calls.

So the question is in 2 parts:

  1. Is it possible to have a delegate from the background thread without retaining the object?
  2. Is this just a bad design? Should I be using NSNotifications instead? Would that be the preferred method of notifying in this case?

Thanks in advance.


A delegate should be retained if there is a possibility that it might be released before any operation on the delegate is invoked. You can set up a state in tableViewController to handle the case when the delegate callback is invoked and the tableViewController is not to be used (Basically make the callbacks act as no-op). Once your operation is done, just release the delegate object.

It is not a bad design but you just need to handle these conditions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号