This article is preceded by: LNDanmakuMaster

LNDanmakuPool is LNDanmakuMaster multiplexing scheme against the spring scene, with UICollectionView/UITableView reuse logic is very similar, for any type of NSObject playing tag, and through the label NSObject type of access.

LNDanmakuPurePool

If we want to implement a pool that can access different objects with different tags, we first need to implement a pool that can access only one fixed type. This pool is LNDanmakuPurePool:

@interface LNDanmakuPurePool : NSObject
@property (nonatomic, assign) NSInteger maxCapacity;
@property (nonatomic, assign) Class targetClass;
@property (nonatomic, copy) NSString *poolKey;
- (void)saveObj:(NSObject *)obj;
- (NSObject *)getObj;
- (void)clearPurePool;
@end
Copy the code

This pool specifies the corresponding relationship between the key and the Class. Only when the key type meets the requirements, the access operation can be performed. The type of the access object is fixed to targetClass. Among other things, the pool defines the maximum amount of storage and the emptying method; If the number of stored objects exceeds the maximum capacity, the storage will not continue. Otherwise, if the current pool is empty, the object is recreated and returns:

- (void)saveObj:(NSObject *)obj { if (self.poolMSet.count > self.maxCapacity) { while (self.poolMSet.count > self.maxCapacity) { [self.poolMSet removeObject:self.poolMSet.anyObject]; } } else if (self.poolMSet.count == self.maxCapacity) { //Fulled . } else { if ([obj isKindOfClass:self.targetClass] && [[obj danmakuPoolKey] isEqualToString:self.poolKey]) { [self.poolMSet addObject:obj]; }}}Copy the code

DanmakuPoolKey is assigned when an object is created in the pool. It is also a flag property of “this object belongs to the pool” (extra judgment in case it works in some unusual cases) :

- (NSObject *)getObj { NSObject *resultObj = nil; while (self.poolMSet.count > 0) { NSObject *obj = [self.poolMSet anyObject]; if ([obj isKindOfClass:self.targetClass] && [obj.danmakuPoolKey isEqualToString:self.poolKey]) { resultObj = obj; [self.poolMSet removeObject:obj]; break; } else { [self.poolMSet removeObject:obj]; } } if (resultObj) { return resultObj; } else { resultObj = [[self.targetClass alloc] init]; [resultObj setDanmakuPoolKey:self.poolKey]; return resultObj; }}Copy the code

LNDanmakuPool

Now that PurePool is defined as a PurePool to call, pools can maintain multiple purepools to access multiple objects. The Pool method is fairly concise:

@interface LNDanmakuPool : NSObject
- (void)registerClass:(Class)class forKey:(NSString *)key;
- (NSObject *)instanceForKey:(NSString *)key;
- (void)saveInstance:(NSObject *)instance;
@end
Copy the code

During the registration process, a PurePool of class type is created for the Key. When a user stores or obtains a PurePool, the user can use this mapping to find the corresponding PurePool for access.

@interface LNDanmakuPool ()
@property (nonatomic, strong) NSMutableDictionary <NSString *, LNDanmakuPurePool *> *poolMDic;
@end
Copy the code

Access what NSObject?

Typically, we have a dedicated Pool for each Player to reuse. In general, NSAttributes inherit from NSObject, like a wrapper, which is one-time and inexpensive and doesn’t need to be reused; CustomObj is a bullet-screen model specified by the business side, which itself is an unfixed ID type. It is not realistic to reuse the business model. Therefore, reuse, like UICollectionView, is UI level reuse: UIView/CAlayer, if the user’s UIView hierarchy is complex and has a lot of layout logic, click events, etc., LNDanmakuMaster recommends getting these UI components from the Player pool, so that the UI components are labeled with the corresponding pool. LNDanmakuMaster is automatically reclaimed at various times (this process does not need to be handled manually before any NSAttributes are invalid, as long as they are registered and retrieved from the Pool), including:

  • When the Dispatcher discards an Attributes from the queue
  • When the Dispatcher is cleaned up
  • After playing, an Attributes has been uninstalled
  • Failed to insert Attributes when the current Player is not playing

Tips

Although LNDanmakuPlayer provides a Pool to get a UI component, it does not specify what must be done with the UI component; Thus, a user can grab a UI component from the Pool but not play it in the player, thus “stealing” a UI component from the LNDanmakuPlayer Pool, because components that have not gone through the danmakuplayer cycle are not recycled.