CADisplayLink at iOS 6.0 not retaining target

落爺英雄遲暮 提交于 2019-12-05 18:36:12

This is useful related information, not an answer.

Rather than use NSInvokation you can use a weak proxy as I have done in my actual answer to this question. It's very simple, here's the code:

JAWeakProxy.h:

#import <Foundation/Foundation.h>

@interface JAWeakProxy : NSObject

@property (weak, nonatomic) id  target;

+ (JAWeakProxy*)weakProxyWithTarget:(id)target;

@end

JAWeakProxy.m:

#import "JAWeakProxy.h"

@implementation JAWeakProxy

+ (JAWeakProxy*)weakProxyWithTarget:(id)target
{
    JAWeakProxy* newObj = [self new];
    newObj.target = target;
    return newObj;
}

- (BOOL)respondsToSelector:(SEL)sel
{
    return [_target respondsToSelector:sel] || [super respondsToSelector:sel];
}

- (id)forwardingTargetForSelector:(SEL)sel
{
    return _target;
}

@end

NOTE: This is ARC code, you'll need to autorelease in weakProxyWithTarget: if not using ARC.

I had the same problem. I actually want a weak reference but since it's documented as strong and behaves that way in other versions of iOS I use a weak proxy object to forward the selector to where I really want it to go. To ensure the proxy object is retained I had to figure out a way to safely retain it on broken versions of iOS without over-retaining it for non-broken versions. I came up with a very elegant one-line solution (the line after the four lines of comments explaining it):

JAWeakProxy*    weakSelf = [JAWeakProxy weakProxyWithTarget:self];
_displayLink = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(displayLinkUpdate:)];
// Due to a bug in iOS 6, CADisplayLink doesn't retain its target (which it should and is
// documented to do) so we need to ensure a strong reference to the weak proxy object is
// created somewhere. We do this by adding it as an associated object to the display link
// which means that it gets retained for as long as the display link object is alive.
objc_setAssociatedObject(_displayLink, @"Retain the target, bitch!", weakSelf, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

Remember to #import <objc/runtime.h>. Using an associated object is great because it gets released when the display link is dealloced and on non-broken versions of the OS it simply means the object is retained twice by the display link.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!