HWWeakTimer.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2015 ( https://github.com/callmewhy )
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #import "HWWeakTimer.h"
  23. @interface HWWeakTimerTarget : NSObject
  24. @property (nonatomic, weak) id target;
  25. @property (nonatomic, assign) SEL selector;
  26. @property (nonatomic, weak) NSTimer* timer;
  27. @end
  28. @implementation HWWeakTimerTarget
  29. - (void) fire:(NSTimer *)timer {
  30. if(self.target) {
  31. #pragma clang diagnostic push
  32. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  33. [self.target performSelector:self.selector withObject:timer.userInfo afterDelay:0.0f];
  34. #pragma clang diagnostic pop
  35. } else {
  36. [self.timer invalidate];
  37. }
  38. }
  39. @end
  40. @implementation HWWeakTimer
  41. + (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
  42. target:(id)aTarget
  43. selector:(SEL)aSelector
  44. userInfo:(id)userInfo
  45. repeats:(BOOL)repeats {
  46. HWWeakTimerTarget* timerTarget = [[HWWeakTimerTarget alloc] init];
  47. timerTarget.target = aTarget;
  48. timerTarget.selector = aSelector;
  49. timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
  50. target:timerTarget
  51. selector:@selector(fire:)
  52. userInfo:userInfo
  53. repeats:repeats];
  54. return timerTarget.timer;
  55. }
  56. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
  57. block:(HWTimerHandler)block
  58. userInfo:(id)userInfo
  59. repeats:(BOOL)repeats {
  60. NSMutableArray *userInfoArray = [NSMutableArray arrayWithObject:[block copy]];
  61. if (userInfo != nil) {
  62. [userInfoArray addObject:userInfo];
  63. }
  64. return [self scheduledTimerWithTimeInterval:interval
  65. target:self
  66. selector:@selector(_timerBlockInvoke:)
  67. userInfo:[userInfoArray copy]
  68. repeats:repeats];
  69. }
  70. + (void)_timerBlockInvoke:(NSArray*)userInfo {
  71. HWTimerHandler block = userInfo[0];
  72. id info = nil;
  73. if (userInfo.count == 2) {
  74. info = userInfo[1];
  75. }
  76. // or `!block ?: block();` @sunnyxx
  77. if (block) {
  78. block(info);
  79. }
  80. }
  81. @end