开启后,WeakProxy NSTimer定时针失效
ZC-Hank opened this issue · 1 comments
直接使用YYTextWeakProxy 定时器还是失效
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[YYTextWeakProxy proxyWithTarget:self] selector:@selector(timerEvent1:) userInfo:nil repeats:YES];
// self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerEvent1:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/**
A proxy used to hold a weak object.
It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
sample code:
@implementation MyView {
NSTimer *_timer;
}
-
(void)initTimer {
YYTextWeakProxy *proxy = [YYTextWeakProxy proxyWithTarget:self];
_timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
} -
(void)tick:(NSTimer *)timer {...}
@EnD
*/
@interface YYTextWeakProxy : NSProxy
/**
The proxy target.
*/
@Property (nullable, nonatomic, weak, readonly) id target;
/**
Creates a new weak proxy for target.
@param target Target object.
@return A new proxy object.
*/
(instancetype)initWithTarget:(id)target;
/**
Creates a new weak proxy for target.
@param target Target object.
@return A new proxy object.
*/
(instancetype)proxyWithTarget:(id)target;
@EnD
#import "YYTextWeakProxy.h"
@implementation YYTextWeakProxy
(instancetype)initWithTarget:(id)target {
_target = target;
return self;
}
(instancetype)proxyWithTarget:(id)target {
return [[YYTextWeakProxy alloc] initWithTarget:target];
}
(id)forwardingTargetForSelector:(SEL)selector {
return _target;
}
(void)forwardInvocation:(NSInvocation *)invocation {
void *null = NULL;
[invocation setReturnValue:&null];
}
(NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}
(BOOL)respondsToSelector:(SEL)aSelector {
return [_target respondsToSelector:aSelector];
}
(BOOL)isEqual:(id)object {
return [_target isEqual:object];
}
(NSUInteger)hash {
return [_target hash];
}
(Class)superclass {
return [_target superclass];
}
(Class)class {
return [_target class];
}
(BOOL)isKindOfClass:(Class)aClass {
return [_target isKindOfClass:aClass];
}
(BOOL)isMemberOfClass:(Class)aClass {
return [_target isMemberOfClass:aClass];
}
(BOOL)conformsToProtocol:(Protocol *)aProtocol {
return [_target conformsToProtocol:aProtocol];
}
(BOOL)isProxy {
return YES;
}
(NSString *)description {
return [_target description];
}
(NSString *)debugDescription {
return [_target debugDescription];
}
因为你用了YYTextWeakProxy
弱引用功能,而且他是通过NSProxy转发的,所以没有selector存在的,target 并没有执行invoke,如果你们团队内统一用YYTextWeakProxy
,可以关闭JJExceptionGuardNSTimer
,这样就不会有问题了