Jichao Jichao - 3 years ago 217
iOS Question

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];
}
});


I have a MRC objective-c file, which need run some code in block asynchronously.

But I thought the
blockself
is not safe since it is not weak pointer, Should I change it to true weak pointer.

@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@property (nonatomic, assign) bool released;
@end
@implementation WeakPtr
@end


set the 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];
}
});
}

Answer Source

Yes, definitely.

1) make a ARC weak pointer wrapper class.

@interface WeakPtr : NSObject
@property (nonatomic, weak) id realObj;
@end
@implementation WeakPtr
@end

2) use the ARC weak pointer wrapper in MRC to wrap the strong pointer.

-(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];
    });
}

Another option is -fobj-weak compiler flag.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download