Rigtht方式来实现Aysnchronous委托方法 [英] Rigtht way to implement Aysnchronous Delegate Methods

查看:177
本文介绍了Rigtht方式来实现Aysnchronous委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的情况。

-(void) foo {

    Object * obj = [[Object alloc] init];

    obj.delegate = self;

    [obj excuteAsync];
}

-(void) delegateMethodReturned {
    // do something
}

下面executeAync返回aynchronously后的某个时间。因此,我们无法安全地释放OBJ。什么是最好的设计模式,来实现这样的情况不宣的obj为伊娃。

Here executeAync returns aynchronously after sometime. Thus we cannot release obj safely. What is the best design pattern to implement such a situation without declaring obj as an iVar.

感谢

推荐答案

如果你可以针对iOS4的,你可以使用模块和GCD规避异步回调。

If you can target iOS4 you could circumvent the asynchronous callback using blocks and GCD.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^ {
    Object * obj = [[Object alloc] init];
    [obj excuteSync];
    // do something
    [obj release];
});

我发现这是很有帮助的在某些情况下,但您的里程可能会有所不同。

I have found this helpful in some situations but your mileage may vary.

这篇关于Rigtht方式来实现Aysnchronous委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆