V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
laobaozi
V2EX  ›  iDev

delegate 的 wek 和 strong 引起的问题

  •  
  •   laobaozi · 2015-10-30 09:27:24 +08:00 · 2545 次点击
    这是一个创建于 3106 天前的主题,其中的信息可能已经有所发展或是发生改变。

    先上代码

    #import "BaseRequest.h"
    
    @protocol RequestDelegate <NSObject>
    
    @optional
    -(void)ReturnRequestData:(NSArray *)dataArray;
    
    @end
    
    @interface GetRequest : BaseRequest
    
    -(int)Get:(NSString *)goalUrl;
    
    @property (nonatomic, weak) id <RequestDelegate> getDelegate;
    @end
    

    在 m 中

    if ([_getDelegate respondsToSelector:@selector(ReturnRequestData:)]) {
    [_getDelegate ReturnRequestData:array];
    }

    问题在 当这个 getDelegate 对象为 wek 时,不能响应方法,而为 strong 时则没有问题.
    但是在别的地方使用代理时将 delegate 设为 weak 又没问题,这个 why?

    8 条回复    2015-10-31 18:28:41 +08:00
    ashamp
        1
    ashamp  
       2015-10-30 09:30:31 +08:00
    delegate 没有持有 BaseRequest 吧?
    request 出了作用于被立即释放了
    lxian2
        2
    lxian2  
       2015-10-30 09:49:58 +08:00 via iPhone
    想吐槽下一个 property 为什么要叫 getDelegate
    laobaozi
        3
    laobaozi  
    OP
       2015-10-30 10:30:58 +08:00
    @ashamp 是的 是对象被清楚了,还在查是哪儿被清除了
    @lxian2 这个 http 请求的 get 类 所有就叫 getDelegate 了..
    matsuijurina
        4
    matsuijurina  
       2015-10-30 10:50:27 +08:00
    我猜十有八九楼主在.m 文件里调用 delegate 指定方法的代码是放在一个 block 里面的。而在你的代码里直接使用了 self. 这时候就出现循环引用的问题了, block 会 retain self , self 又会 retain block 。 解决的方法有很多种,最直接的就是先利用 __weak 把 self 转成 weakSelf ,在 block 内部需要使用 self 的地方都换成 weakSelf ,这样就没有问题了。
    matsuijurina
        5
    matsuijurina  
       2015-10-30 11:14:42 +08:00
    还有一个办法是你直接在 block 的外面加一行代码

    id tempGetDelegate = self.getDelegate;

    然后把 block 内的代码里替换为
    if ([tempGetDelegate respondsToSelector:@selector(ReturnRequestData:)]) {
    [tempGetDelegate ReturnRequestData:array];
    }


    这样也可以。这种情况下 self 会 retain block , block retain delegate ,就没有循环引用了。
    loveuqian
        6
    loveuqian  
       2015-10-30 12:25:45 +08:00
    我在 m 里面是用 get 方法去拿代理的,不是用下划线成员变量
    if ([self.getDelegate respondsToSelector:@selector(ReturnRequestData:)])
    {
    [self.getDelegate ReturnRequestData:array];
    }
    laobaozi
        7
    laobaozi  
    OP
       2015-10-30 13:24:50 +08:00
    @matsuijurina 是的 我的确的放在了 NSURLSessionTask 的回调里面,造成了循环引用
    谢谢你指导
    lcl22hope
        8
    lcl22hope  
       2015-10-31 18:28:41 +08:00
    因为你的 delegate 没有实例化,_getDelegate 是 nil ,两点原因: 1.需要实现代理协议的地方没有实现代理,比如类似 tableView.delegate = self 这种。 2.方法本身没有对 delegate ,调用 setDelegate
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2086 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 16:05 · PVG 00:05 · LAX 09:05 · JFK 12:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.