Skip to content

Commit

Permalink
fix: buildDynamicGeneralProps logic
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Apr 19, 2024
1 parent 8fd746d commit 646c7a5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion GrowingTrackerCore/Event/GrowingGeneralProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)sharedInstance;

- (NSDictionary<NSString *, NSString *> *)getGeneralProps;
- (NSDictionary<NSString *, id> *)getGeneralProps;

- (void)setGeneralProps:(NSDictionary<NSString *, id> *)props;

Expand Down
39 changes: 27 additions & 12 deletions GrowingTrackerCore/Event/GrowingGeneralProps.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

@interface GrowingGeneralProps ()

@property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *internalProps;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *dynamicProps;
@property (nonatomic, copy) NSDictionary<NSString *, NSString *> * (^dynamicPropsBlock)(void);
@property (nonatomic, strong) NSMutableDictionary<NSString *, id> *internalProps;
@property (atomic, copy) NSDictionary<NSString *, id> *dynamicProps;
@property (nonatomic, copy) NSDictionary<NSString *, id> * (^dynamicPropsBlock)(void);

@end

Expand All @@ -49,15 +49,26 @@ + (instancetype)sharedInstance {
return instance;
}

- (NSDictionary<NSString *, NSString *> *)getGeneralProps {
__block NSDictionary *props = nil;
GROWING_RW_LOCK_READ(lock, props, ^{
// dynamic general properties > general properties
NSMutableDictionary *properties = self.internalProps.mutableCopy;
[properties addEntriesFromDictionary:self.dynamicProps];
return properties;
- (NSDictionary<NSString *, id> *)getGeneralProps {
if (!self.dynamicProps && self.dynamicPropsBlock) {
// 动态属性未build
[self buildDynamicGeneralProps];
}

__block NSMutableDictionary *properties = nil;
GROWING_RW_LOCK_READ(lock, properties, ^{
return self.internalProps.mutableCopy;
});
return [props copy];

// dynamic general properties > general properties
if (self.dynamicProps) {
[properties addEntriesFromDictionary:self.dynamicProps];
}

// 置为nil,保证下一次事件能够获取最新值
self.dynamicProps = nil;

return [properties copy];
}

- (void)setGeneralProps:(NSDictionary<NSString *, id> *)props {
Expand Down Expand Up @@ -92,20 +103,24 @@ - (void)registerDynamicGeneralPropsBlock:
}

- (void)buildDynamicGeneralProps {
// 一般情况下,buildDynamicGeneralProps应该在用户线程中调用,以获取实际值
// 目前有:首次初始化SDK、setLoginUserId、setDataCollectionEnabled(皆对应VISIT事件)
// 其他非必要的场景则在事件创建过程中调用,也就是在GrowingThread
GROWING_RW_LOCK_READ(lock, self.dynamicProps, ^{
if (self.dynamicPropsBlock) {
NSDictionary *dynamicProps = self.dynamicPropsBlock();
if (dynamicProps && [dynamicProps isKindOfClass:[NSDictionary class]]) {
return (NSDictionary *)[dynamicProps copy];
}
}
// always return not nil value
return @{};
});
}

#pragma mark - Setter && Getter

- (NSMutableDictionary<NSString *, NSString *> *)internalProps {
- (NSMutableDictionary<NSString *, id> *)internalProps {
if (!_internalProps) {
_internalProps = [NSMutableDictionary dictionary];
}
Expand Down
9 changes: 6 additions & 3 deletions GrowingTrackerCore/GrowingRealTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ - (instancetype)initWithConfiguration:(GrowingTrackConfiguration *)configuration
// 各个Module初始化init之后再进行事件定时发送
[[GrowingEventManager sharedInstance] configManager];
[[GrowingEventManager sharedInstance] startTimerSend];
// 初始化SDK时获取一次动态通用属性
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[self versionPrint];
[self filterLogPrint];
}
Expand Down Expand Up @@ -215,27 +217,28 @@ - (void)clearGeneralProps {
}

- (void)setLoginUserId:(NSString *)userId {
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[GrowingDispatchManager dispatchInGrowingThread:^{
[[GrowingSession currentSession] setLoginUserId:userId];
}];
}

/// 支持设置userId的类型, 存储方式与userId保持一致, userKey默认为null
/// @param userId 用户ID
/// @param userKey 用户ID对应的key值
- (void)setLoginUserId:(NSString *)userId userKey:(NSString *)userKey {
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[GrowingDispatchManager dispatchInGrowingThread:^{
[[GrowingSession currentSession] setLoginUserId:userId userKey:userKey];
}];
}

- (void)cleanLoginUserId {
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[GrowingDispatchManager dispatchInGrowingThread:^{
[[GrowingSession currentSession] setLoginUserId:nil];
}];
}

- (void)setDataCollectionEnabled:(BOOL)enabled {
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[GrowingDispatchManager dispatchInGrowingThread:^{
GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration;
if (enabled == trackConfiguration.dataCollectionEnabled) {
Expand Down

0 comments on commit 646c7a5

Please sign in to comment.