首页 > 其他 > 详细

weakRefrence 到Invocation(一)

时间:2016-01-02 00:50:36      阅读:247      评论:0      收藏:0      [点我收藏+]

   最近在工作中遇到问题,就是我的代理层有两个方法 一个是添加代理 addDelegate:(id)delegate onQueue:(dispatch_queue_t)queue; 一个是删除代理 removeDelegate:(id)delegate;我都是添加到数组中。如果只是普通的添加会导致实现实例无法释放。

  于是就想方法把添加的实例的弱引用添加到数组。开始我是这样弄:

WeakRefrenceObject makeWeakRefrenceObject(id object){
    __weak typeof(object)weakObject = object;
    return ^id{
        return weakObject;
    };
}

需要获取到对象就这样弄:

id getObjectOnWeakReferenceObject(WeakReferenceObject ref){
    if (ref == NULL) return nil;
    else
        return ref();
}

使用弱指针将对象地址保存到Block中,然后添加到数组。

  因为还需要根据每个线程发送消息。所以我们进一步进化:

@interface LIPDelegateNode :NSObject
@property (nonatomic, weak) id delegate;
@property (nonatomic, assign) dispatch_queue_t onQueue;
- (instancetype)initWithDelegate:(id)delegate onQueue:(dispatch_queue_t)queue;
@end
@implementation LIPDelegateNode
- (instancetype)initWithDelegate:(id)delegate onQueue:(dispatch_queue_t)queue{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _onQueue = queue;
    }
    return self;
}
- (instancetype)init{
    return [self initWithDelegate:nil onQueue:NULL];
}
@end

 

weakRefrence 到Invocation(一)

原文:http://www.cnblogs.com/simple-life-no1/p/5093503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!