diff --git a/GrowingTrackerCore/Event/GrowingGeneralProps.h b/GrowingTrackerCore/Event/GrowingGeneralProps.h index d00a4cc6..ada6693b 100644 --- a/GrowingTrackerCore/Event/GrowingGeneralProps.h +++ b/GrowingTrackerCore/Event/GrowingGeneralProps.h @@ -35,8 +35,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)setDynamicGeneralPropsGenerator:(NSDictionary * (^_Nullable)(void))generator; -- (void)buildDynamicGeneralProps; - @end NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Event/GrowingGeneralProps.m b/GrowingTrackerCore/Event/GrowingGeneralProps.m index 3e2e1d56..600de5d9 100644 --- a/GrowingTrackerCore/Event/GrowingGeneralProps.m +++ b/GrowingTrackerCore/Event/GrowingGeneralProps.m @@ -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 *internalProps; -@property (atomic, copy) NSDictionary *dynamicProps; @property (nonatomic, copy) NSDictionary * (^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; @@ -50,24 +40,20 @@ + (instancetype)sharedInstance { } - (NSDictionary *)getGeneralProps { - if (!self.dynamicProps && self.dynamicPropsGenerator) { - // 动态属性未build - [self buildDynamicGeneralProps]; + // running on GrowingThread + NSDictionary *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]; } @@ -75,46 +61,30 @@ - (void)setGeneralProps:(NSDictionary *)props { if ([GrowingArgumentChecker isIllegalAttributes:props]) { return; } - GROWING_RW_LOCK_WRITE(lock, ^{ + [GrowingDispatchManager dispatchInGrowingThread:^{ [self.internalProps addEntriesFromDictionary:props]; - }); + }]; } - (void)removeGeneralProps:(NSArray *)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 * (^_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 diff --git a/GrowingTrackerCore/GrowingRealTracker.m b/GrowingTrackerCore/GrowingRealTracker.m index 025bdd42..31953742 100644 --- a/GrowingTrackerCore/GrowingRealTracker.m +++ b/GrowingTrackerCore/GrowingRealTracker.m @@ -74,8 +74,6 @@ - (instancetype)initWithConfiguration:(GrowingTrackConfiguration *)configuration // 各个Module初始化init之后再进行事件定时发送 [[GrowingEventManager sharedInstance] configManager]; [[GrowingEventManager sharedInstance] startTimerSend]; - // 初始化SDK时获取一次动态通用属性 - [[GrowingGeneralProps sharedInstance] buildDynamicGeneralProps]; [self versionPrint]; [self filterLogPrint]; } @@ -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) {