Note that there are some explanatory texts on larger screens.

plurals
  1. POKeep object reference of local variable in ARC
    text
    copied!<p>I have created a class under ARC with some methods that accepts blocks. The problem is app keep crashing, and I think the reason of crash is the object is getting released by ARC. My question is, how can I fix this, i'e how can I keep the reference of object so the object wont be released until the block has been processed. </p> <p><strong>Here is .h class</strong></p> <pre><code>#if NS_BLOCKS_AVAILABLE typedef void (^KelaMagicalControlCompletionBlock)(); #endif @interface KelaMagicalControl : NSObject +(KelaMagicalControl *)controlWithTitle:(NSString *)title message:(NSString *)message; -(id)initWithTitle:(NSString *)title message:(NSString *)message; -(void)showWithTouchCompletionBlock:(KelaMagicalControlCompletionBlock)completionBlock; @end </code></pre> <p><strong>Here is .m class</strong></p> <pre><code>#import "KelaMagicalControl.h" @interface KelaMagicalControl() @property (nonatomic, strong) NSString * title; @property (nonatomic, strong) NSString * message; @property (copy) KelaMagicalControlCompletionBlock completionBlock; @end @implementation KelaMagicalControl -(void)dealloc { NSLog(@"deallocated"); } + (KelaMagicalControl *)toastWithTitle:(NSString *)title message:(NSString *)message { KelaMagicalControl * magicalControl = [[KelaMagicalControl alloc] initWithTitle:title message:message]; return magicalControl; } -(id)initWithTitle:(NSString *)title message:(NSString *)message { if(self = [super init]) { _title = title; _message = message; } return self; } -(void)showWithTouchCompletionBlock:(void (^)())completionBlock { UIWindow * mainWindow = [[UIApplication sharedApplication]keyWindow]; UIView * viewTemp = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 100)]; [viewTemp setTag:10001]; [viewTemp setBackgroundColor:[UIColor redColor]]; [mainWindow addSubview:viewTemp]; UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainViewTapped)]; [viewTemp addGestureRecognizer:tapGestureRecognizer]; self.completionBlock = completionBlock; } -(void)mainViewTapped { if(self.completionBlock) { self.completionBlock(); self.completionBlock = nil; } } </code></pre> <p>From controller class, I send message to custom class' methods like this:</p> <pre><code>-(IBAction)showMagicalControl:(id)sender { NSString * title = @"Title"; NSString * message = @"This is a very long message"; KelaMagicalControl * magicalControl = [[KelaMagicalControl alloc] initWithTitle:title message:message]; [magicalControl showWithTouchCompletionBlock:^{ NSLog(@"control tapped"); }]; } </code></pre> <p>So it shows the control fine, but when I tap on it, instead of executing block, it crashes with error "obj_msgsend". It doesn't even reach on showMagicalControl method. I think, as I am using ARC, it is getting released automatically, I can see dealloc gets called immediately (before executing block). It doesn't crash if I create a property of the magicalRecord and use it, but as per my requirement, I dont want to use a global iVar or property just to call this code of block. </p> <p>Any suggestions please?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload