开发者

why retain of delegate is wrong what are all alternatives...?

I have one problem let assume A and B are 2 view controller from A user push to B view controller,In B user starts some download by creating object C(which is NSObject class) and sets B as delegate to C(assign),now user want go back to A then dealloc of B calls object releases, C delegate fails to give call back(crashes).I want to get call and allow user to move to other view contro开发者_如何学Goller thats way i am retain the delegate in C class but retain of delegate is wrong ...

what are all solutions ...

Thanks in Advance.


I see two options:

  1. You do not need the downloaded data. Solution: set the delegate to nil.
  2. You need the data. Solution: You either set a new delegate that exists, or the delegate should make sure it will be around (f.e. by being a singleton)


That happens because of an issue in your architecture: you're assigning the global task of background downloading to a view controller that may or may not be in memory.

If you want to be able to continue the download regardless of the presence of B, then:

  1. Create a class "downloader" that takes care of downloading files (I believe you call it C in your example).
  2. Such class should have a delegate of type "weak" so that if the original delegate goes away, the app won't crash (NOTE: this is only available if you use ARC). If you need to globally monitor the progress of your download, switch to NSNotification instead of delegate so that multiple object can monitor at the same time.
  3. Initialize an instance of C somewhere else: either in A or before that.
  4. Inject the instance of C into B so that B by creating a custom init method or a @property.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜