如何在MRC中使自己变弱?
How to make weak self in MRC?
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block ProtocolDriver* blockSelf = self;
dispatch_async(queue, ^{
if (blockSelf.protocol_delegate && [blockSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[blockSelf.protocol_delegate onReceive:m];
}
});
我有一个 MRC objective-c 文件,它需要 运行 异步块中的一些代码。
但是我认为blockself
不安全,因为它不是弱指针,我是否应该将其更改为真正的弱指针。
@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@property (nonatomic, assign) bool released;
@end
@implementation WeakPtr
@end
设置released when the MRC-object is dealloced, and test it in block
@property (nonatomic, retain) WeakPtr* selfPtr;
- (id)init{
...
self.selfPtr = [[[WeakPtr alloc] init] autorelease];
self.selfPtr.realObj = self;
}
- (void)dealloc
{
...
self.selfPtr.released = true;
}
-(void)pump: (Message *)msg
{
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block WeakPtr* weakSelf = self.selfPtr;
dispatch_async(queue, ^{
if (!weakSelf.released && [weakSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[weakSelf.protocol_delegate onReceive:m];
}
});
}
简答:你不能。 weak
限定符是 ARC 的一个特性。在MRC中,类型是assign,相当于ARC的unsafe_unretained
类型。
MRC 没有 auto-niling 弱类型。
编辑:
好吧,也许“你不能”太强了。最好说:
MRC 不支持自动 niling 弱引用,您必须使用应用程序级代码或为您执行此操作的库来处理它。
是的,绝对是。
1) 制作一个 ARC 弱指针包装器 class.
@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@end
@implementation WeakPtr
@end
2) 使用MRC中的ARC弱指针包装器来包装强指针
-(void)pump: (Message *)msg
{
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
WeakPtr* weakPtr = [WeakPtr new];
weakPtr.realObj = self;
dispatch_async(queue, ^{
if ([weakPtr.realObj.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[weakPtr.realObj.protocol_delegate onReceive:m];
}
[weakPtr release];
});
}
另一个选项是 -fobj-weak
编译器标志。
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block ProtocolDriver* blockSelf = self;
dispatch_async(queue, ^{
if (blockSelf.protocol_delegate && [blockSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[blockSelf.protocol_delegate onReceive:m];
}
});
我有一个 MRC objective-c 文件,它需要 运行 异步块中的一些代码。
但是我认为blockself
不安全,因为它不是弱指针,我是否应该将其更改为真正的弱指针。
@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@property (nonatomic, assign) bool released;
@end
@implementation WeakPtr
@end
设置released when the MRC-object is dealloced, and test it in block
@property (nonatomic, retain) WeakPtr* selfPtr;
- (id)init{
...
self.selfPtr = [[[WeakPtr alloc] init] autorelease];
self.selfPtr.realObj = self;
}
- (void)dealloc
{
...
self.selfPtr.released = true;
}
-(void)pump: (Message *)msg
{
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
__block WeakPtr* weakSelf = self.selfPtr;
dispatch_async(queue, ^{
if (!weakSelf.released && [weakSelf.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[weakSelf.protocol_delegate onReceive:m];
}
});
}
简答:你不能。 weak
限定符是 ARC 的一个特性。在MRC中,类型是assign,相当于ARC的unsafe_unretained
类型。
MRC 没有 auto-niling 弱类型。
编辑:
好吧,也许“你不能”太强了。最好说:
MRC 不支持自动 niling 弱引用,您必须使用应用程序级代码或为您执行此操作的库来处理它。
是的,绝对是。
1) 制作一个 ARC 弱指针包装器 class.
@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@end
@implementation WeakPtr
@end
2) 使用MRC中的ARC弱指针包装器来包装强指针
-(void)pump: (Message *)msg
{
Message *m = [msg retain];
dispatch_queue_t queue = _handle_queue;
WeakPtr* weakPtr = [WeakPtr new];
weakPtr.realObj = self;
dispatch_async(queue, ^{
if ([weakPtr.realObj.protocol_delegate respondsToSelector:@selector(onReceive:)]) {
[weakPtr.realObj.protocol_delegate onReceive:m];
}
[weakPtr release];
});
}
另一个选项是 -fobj-weak
编译器标志。