iOS 6.0中的CADisplayLink不保留目标

时间:2012-09-21 14:19:21

标签: iphone ios ios6 cadisplaylink

我有这样的代码:

NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(updateFrame)]];
[invocation setTarget:self];
[invocation setSelector:@selector(updateFrame)];
displayLink_ = [[CADisplayLink displayLinkWithTarget:invocation selector:@selector(invoke)] retain];
[displayLink_ setFrameInterval:1];
[displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

在iOS 6.0(在5.1中这段代码可以正常工作)当这段代码调用我有两个变种时:EXC_BAD_ACCESS或'调用无法识别的选择器'调用“'。似乎displayLinkWithTarget:selector:方法不保留目标。当我添加[invocation retain]行时,代码变得正常工作。这是iOS 6.0的错误吗?

2 个答案:

答案 0 :(得分:4)

这是有用的相关信息,而不是答案。

而不是使用NSInvokation,您可以使用弱代理,就像我在这个问题的实际答案中所做的那样。这很简单,这是代码:

<强> 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

注意:这是ARC代码,如果不使用ARC,则需要autorelease weakProxyWithTarget:

答案 1 :(得分:2)

我遇到了同样的问题。我实际上想要弱引用但是由于它被记录为强大并且在其他版本的iOS中表现得那么我使用弱代理对象将选择器转发到我真正希望它去的地方。为了确保保留代理对象,我必须找到一种方法来安全地将其保留在破坏的iOS版本上,而不会为非破坏版本过度保留它。我提出了一个非常优雅的单行解决方案(之后的行四行评论解释它):

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

请记住#import <objc/runtime.h>。使用关联对象很好,因为它在显示链接dealloc时被释放,而在OS的非破坏版本上它只是意味着对象被显示链接保留两次。