Skip to content

Commit

Permalink
fix: generalProps in multi-threads
Browse files Browse the repository at this point in the history
  • Loading branch information
YoloMao committed Apr 23, 2024
1 parent 03faccd commit 3453f3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 58 deletions.
2 changes: 0 additions & 2 deletions GrowingTrackerCore/Event/GrowingGeneralProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ NS_ASSUME_NONNULL_BEGIN

- (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator;

- (void)buildDynamicGeneralProps;

@end

NS_ASSUME_NONNULL_END
70 changes: 20 additions & 50 deletions GrowingTrackerCore/Event/GrowingGeneralProps.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,16 @@

#import "GrowingTrackerCore/Event/GrowingGeneralProps.h"
#import "GrowingTrackerCore/Utils/GrowingArgumentChecker.h"
#import "GrowingTrackerCore/Utils/GrowingInternalMacros.h"
#import "GrowingTrackerCore/Thread/GrowingDispatchManager.h"

@interface GrowingGeneralProps ()

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

@end

@implementation GrowingGeneralProps {
GROWING_RW_LOCK_DECLARE(lock);
}

- (instancetype)init {
if (self = [super init]) {
GROWING_RW_LOCK_INIT(lock);
}
return self;
}
@implementation GrowingGeneralProps

+ (instancetype)sharedInstance {
static GrowingGeneralProps *instance;
Expand All @@ -50,71 +40,51 @@ + (instancetype)sharedInstance {
}

- (NSDictionary<NSString *, id> *)getGeneralProps {
if (!self.dynamicProps && self.dynamicPropsGenerator) {
// 动态属性未build
[self buildDynamicGeneralProps];
// running on GrowingThread
NSDictionary<NSString *, id> *dynamicProps = nil;
if (self.dynamicPropsGenerator) {
NSDictionary *dic = self.dynamicPropsGenerator();
if (dic && [dic isKindOfClass:[NSDictionary class]]) {
dynamicProps = (NSDictionary *)[dic copy];
}
}

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

NSMutableDictionary *properties = self.internalProps.mutableCopy;
// dynamic general properties > general properties
if (self.dynamicProps) {
[properties addEntriesFromDictionary:self.dynamicProps];
if (dynamicProps) {
[properties addEntriesFromDictionary:dynamicProps];
}

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

return [properties copy];
}

- (void)setGeneralProps:(NSDictionary<NSString *, id> *)props {
if ([GrowingArgumentChecker isIllegalAttributes:props]) {
return;
}
GROWING_RW_LOCK_WRITE(lock, ^{
[GrowingDispatchManager dispatchInGrowingThread:^{
[self.internalProps addEntriesFromDictionary:props];
});
}];
}

- (void)removeGeneralProps:(NSArray<NSString *> *)keys {
if ([GrowingArgumentChecker isIllegalKeys:keys]) {
return;
}
GROWING_RW_LOCK_WRITE(lock, ^{
[GrowingDispatchManager dispatchInGrowingThread:^{
[self.internalProps removeObjectsForKeys:keys];
});
}];
}

- (void)clearGeneralProps {
GROWING_RW_LOCK_WRITE(lock, ^{
[GrowingDispatchManager dispatchInGrowingThread:^{
[self.internalProps removeAllObjects];
});
}];
}

- (void)setDynamicGeneralPropsGenerator:(NSDictionary<NSString *, id> * (^_Nullable)(void))generator {
GROWING_RW_LOCK_WRITE(lock, ^{
[GrowingDispatchManager dispatchInGrowingThread:^{
self.dynamicPropsGenerator = generator;
});
}

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

#pragma mark - Setter && Getter
Expand Down
6 changes: 0 additions & 6 deletions GrowingTrackerCore/GrowingRealTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ - (instancetype)initWithConfiguration:(GrowingTrackConfiguration *)configuration
// 各个Module初始化init之后再进行事件定时发送
[[GrowingEventManager sharedInstance] configManager];
[[GrowingEventManager sharedInstance] startTimerSend];
// 初始化SDK时获取一次动态通用属性
[[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps];
[self versionPrint];
[self filterLogPrint];
}
Expand Down Expand Up @@ -217,28 +215,24 @@ - (void)clearGeneralProps {
}

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

- (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 3453f3b

Please sign in to comment.