Re: [问题] block retain 问题

楼主: darktt (小朱)   2016-05-16 22:17:01
※ 引述《issuemylove (NotLove)》之铭言:
: 各位好
: 我利用AFNetworking写了个重复地询问server状态的连线(程式码在下行网址)
: http://pastebin.com/AYUDgK2j
: 想问说,一般网络上看到的情况是
: viewController A retain 一个 block B
: B 中用到 A 时要使用__weak才不会造成memory leak
: 程式码是我写的,可是我搞不清楚这样写对不对
: 感觉是 AFNetworking A, retain Block B, B 再 retain 自己写的Api C
: 然后 C 又retain B,...其实我搞糊涂了真正的retain方向没个概念
: 另外我这样写是正确的吗? 谢谢各位出手相救!
我举个例子:有 FirstViewController 与 SecondViewController,接口如下面网址:
https://gist.github.com/Darktt/b42ddc12d9ae049f0431f93775a756de
FirstViewController 会在其中一次的按钮触发下建立 SecondViewController,
并将 FirstViewController 给了 SecondViewController,
之后将它显示出来。
这时候 FirstViewController 其实还在内存中,不会被释放,
所以不用再使用 retain (strong) 将 FirstViewController 增加一次的 retain count,
这时候在 SecondViewController dealloc 的时候也不能释放 FirstViewController,
因为这会造成提早释放的问题。
其实 assign(weak) 与 retain(strong) 的差异是这个物件有没有被其他的物件长期持有
有的话就是 assign(weak),没有就是 retain(strong)。
作者: issuemylove (NotLove)   2016-05-17 11:01:00
感谢d大解说,脑袋更清晰了 谢谢!
作者: xinya707 (妈我在这)   2016-09-17 14:40:00
Days matter 倒数忌日都有记日
作者: Decill (inner peace)   2016-09-17 14:44:00
一楼我知道是输入法问题 不过我还是忍不住笑了
作者: happyfuture (!)   2016-09-17 14:46:00
1F好吓人阿XD

Links booklink

Contact Us: admin [ a t ] ucptt.com