Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

开启后,WeakProxy NSTimer定时针失效 #97

Open
ZC-Hank opened this issue Feb 22, 2021 · 1 comment
Open

开启后,WeakProxy NSTimer定时针失效 #97

ZC-Hank opened this issue Feb 22, 2021 · 1 comment

Comments

@ZC-Hank
Copy link

ZC-Hank commented Feb 22, 2021

直接使用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];
}

@EnD

@jezzmemo
Copy link
Owner

jezzmemo commented Feb 22, 2021

因为你用了YYTextWeakProxy 弱引用功能,而且他是通过NSProxy转发的,所以没有selector存在的,target 并没有执行invoke,如果你们团队内统一用YYTextWeakProxy,可以关闭JJExceptionGuardNSTimer,这样就不会有问题了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants