NTESService.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // NIMManager.m
  3. // NIM
  4. //
  5. // Created by amao on 11/13/13.
  6. // Copyright (c) 2013 Netease. All rights reserved.
  7. //
  8. #import "NTESService.h"
  9. #import "NTESSessionUtil.h"
  10. #pragma mark - NIMServiceManagerImpl
  11. @interface NIMServiceManagerImpl : NSObject
  12. @property (nonatomic,strong) NSString *key;
  13. @property (nonatomic,strong) NSMutableDictionary *singletons;
  14. @end
  15. @implementation NIMServiceManagerImpl
  16. + (NIMServiceManagerImpl *)coreImpl:(NSString *)key
  17. {
  18. NIMServiceManagerImpl *impl = [[NIMServiceManagerImpl alloc]init];
  19. impl.key = key;
  20. return impl;
  21. }
  22. - (id)init
  23. {
  24. if (self = [super init])
  25. {
  26. _singletons = [[NSMutableDictionary alloc]init];
  27. }
  28. return self;
  29. }
  30. - (instancetype)singletonByClass:(Class)singletonClass
  31. {
  32. NSString *singletonClassName = NSStringFromClass(singletonClass);
  33. id singleton = [_singletons objectForKey:singletonClassName];
  34. if (!singleton) {
  35. singleton = [[singletonClass alloc]init];
  36. [_singletons setObject:singleton forKey:singletonClassName];
  37. }
  38. return singleton;
  39. }
  40. - (void)callSingletonSelector: (SEL)selecotr
  41. {
  42. NSArray *array = [_singletons allValues];
  43. for (id obj in array)
  44. {
  45. if ([obj respondsToSelector:selecotr])
  46. {
  47. SuppressPerformSelectorLeakWarning([obj performSelector:selecotr]);
  48. }
  49. }
  50. }
  51. @end
  52. #pragma mark - NIMServiceManager()
  53. @interface NTESServiceManager ()
  54. @property (nonatomic,strong) NSRecursiveLock *lock;
  55. @property (nonatomic,strong) NIMServiceManagerImpl *core;
  56. + (instancetype)sharedManager;
  57. - (id)singletonByClass:(Class)singletonClass;
  58. @end
  59. #pragma mark - NIMService
  60. @implementation NTESService
  61. + (instancetype)sharedInstance
  62. {
  63. return [[NTESServiceManager sharedManager] singletonByClass:[self class]];
  64. }
  65. - (void)start
  66. {
  67. DDLogDebug(@"NIMService %@ Started", self);
  68. }
  69. @end
  70. #pragma mark - NIMServiceManager
  71. @implementation NTESServiceManager
  72. + (instancetype)sharedManager
  73. {
  74. static NTESServiceManager *instance;
  75. static dispatch_once_t onceToken;
  76. dispatch_once(&onceToken, ^{
  77. instance = [[NTESServiceManager alloc]init];
  78. });
  79. return instance;
  80. }
  81. - (id)init
  82. {
  83. if (self = [super init]) {
  84. [[NSNotificationCenter defaultCenter] addObserver:self
  85. selector:@selector(callReceiveMemoryWarning)
  86. name:UIApplicationDidReceiveMemoryWarningNotification
  87. object:nil];
  88. [[NSNotificationCenter defaultCenter] addObserver:self
  89. selector:@selector(callEnterBackground)
  90. name:UIApplicationDidEnterBackgroundNotification
  91. object:nil];
  92. [[NSNotificationCenter defaultCenter] addObserver:self
  93. selector:@selector(callEnterForeground)
  94. name:UIApplicationWillEnterForegroundNotification
  95. object:nil];
  96. [[NSNotificationCenter defaultCenter] addObserver:self
  97. selector:@selector(callAppWillTerminate)
  98. name:UIApplicationWillTerminateNotification
  99. object:nil];
  100. }
  101. return self;
  102. }
  103. - (void)dealloc
  104. {
  105. [[NSNotificationCenter defaultCenter] removeObserver:self];
  106. }
  107. - (void)start {
  108. [_lock lock];
  109. NSString *key = [[[NIMSDK sharedSDK] loginManager] currentAccount];
  110. _core = [NIMServiceManagerImpl coreImpl:key];
  111. [_lock unlock];
  112. }
  113. - (void)destory {
  114. [_lock lock];
  115. [self callSingletonClean];
  116. _core = nil;
  117. [_lock unlock];
  118. }
  119. - (id)singletonByClass: (Class)singletonClass
  120. {
  121. id instance = nil;
  122. [_lock lock];
  123. instance = [_core singletonByClass:singletonClass];
  124. [_lock unlock];
  125. return instance;
  126. }
  127. #pragma mark - Call Functions
  128. - (void)callSingletonClean
  129. {
  130. [self callSelector:@selector(onCleanData)];
  131. }
  132. - (void)callReceiveMemoryWarning
  133. {
  134. [self callSelector:@selector(onReceiveMemoryWarning)];
  135. }
  136. - (void)callEnterBackground
  137. {
  138. [self callSelector:@selector(onEnterBackground)];
  139. }
  140. - (void)callEnterForeground
  141. {
  142. [self callSelector:@selector(onEnterForeground)];
  143. }
  144. - (void)callAppWillTerminate
  145. {
  146. [self callSelector:@selector(onAppWillTerminate)];
  147. }
  148. - (void)callSelector: (SEL)selector
  149. {
  150. [_core callSingletonSelector:selector];
  151. }
  152. @end