KSYWeakProxy.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // KSYGPUViewCapture.h
  3. // KSYStreamer
  4. //
  5. // Created by yiqian on 1/30/16.
  6. // Copyright © 2016 yiqian. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /**
  11. A proxy used to hold a weak object.
  12. It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink.
  13. sample code:
  14. @implementation ViewController {
  15. NSTimer *_timer;
  16. }
  17. - (void)initTimer {
  18. KSYWeakProxy *proxy = [KSYWeakProxy proxyWithTarget:self];
  19. _timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES];
  20. }
  21. - (void)tick:(NSTimer *)timer {...}
  22. @end
  23. */
  24. @interface KSYWeakProxy : NSProxy
  25. /**
  26. The proxy target.
  27. */
  28. @property (nullable, nonatomic, weak, readonly) id target;
  29. /**
  30. Creates a new weak proxy for target.
  31. @param target Target object.
  32. @return A new proxy object.
  33. */
  34. - (instancetype)initWithTarget:(id)target;
  35. /**
  36. Creates a new weak proxy for target.
  37. @param target Target object.
  38. @return A new proxy object.
  39. */
  40. + (instancetype)proxyWithTarget:(id)target;
  41. @end
  42. NS_ASSUME_NONNULL_END