Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>I still think it would be better to subclass your class and not mess with the runtime, but if you are definitely sure you need to do it in a category, I have an option in mind for you. It still messes with the runtime, but is safer than swizzling I think.</em></p> <p>Consider writing a helper class, say calling it <code>DeallocHook</code> which can be attached to any <code>NSObject</code> and perform an action when this <code>NSObject</code> gets deallocated. Then you can do something like this:</p> <pre><code>// Instead of directly messing with your class -dealloc method, attach // the hook to your instance and do the cleanup in the callback [DeallocHook attachTo: yourObject callback: ^{ [NSObject cancelPrevious... /* your code here */ ]; }]; </code></pre> <p>You can implement the <code>DeallocHook</code> using <code>objc_setAssociatedObject</code>:</p> <pre><code>@interface DeallocHook : NSObject @property (copy, nonatomic) dispatch_block_t callback; + (id) attachTo: (id) target callback: (dispatch_block_t) block; @end </code></pre> <p>Implementation would be something like this:</p> <pre><code>#import "DeallocHook.h" #import &lt;objc/runtime.h&gt; // Address of a static global var can be used as a key static void *kDeallocHookAssociation = &amp;kDeallocHookAssociation; @implementation DeallocHook + (id) attachTo: (id) target callback: (dispatch_block_t) block { DeallocHook *hook = [[DeallocHook alloc] initWithCallback: block]; // The trick is that associations are released when your target // object gets deallocated, so our DeallocHook object will get // deallocated right after your object objc_setAssociatedObject(target, kDeallocHookAssociation, hook, OBJC_ASSOCIATION_RETAIN_NONATOMIC); return hook; } - (id) initWithCallback: (dispatch_block_t) block { self = [super init]; if (self != nil) { // Here we just copy the callback for later self.callback = block; } return self; } - (void) dealloc { // And we place our callback within the -dealloc method // of your helper class. if (self.callback != nil) dispatch_async(dispatch_get_main_queue(), self.callback); } @end </code></pre> <p>See Apple's documentation on <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html" rel="noreferrer">Objective-C runtime</a> for more info about the associative references (although I'd say the docs are not very detailed regarding this subject).</p> <p>I've not tested this thoroughly, but it seemed to work. Just thought I'd give you another direction to look into.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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