@@ -1848,122 +1848,125 @@ + (void)maybePerformActions:(NSArray *)whenConditions
18481848 withContextualValues : (LPContextualValues *)contextualValues
18491849{
18501850 NSDictionary *messages = [[LPVarCache sharedCache ] messages ];
1851- NSMutableArray *actionContexts = [NSMutableArray array ];
1852-
1853- for (NSString *messageId in [messages allKeys ]) {
1854- if (sourceMessage != nil && [messageId isEqualToString: sourceMessage]) {
1855- continue ;
1856- }
1857- NSDictionary *messageConfig = messages[messageId];
1858- NSString *actionType = messageConfig[@" action" ];
1859- if (![actionType isKindOfClass: NSString .class]) {
1860- continue ;
1861- }
18621851
1863- NSString *internalMessageId;
1864- if ([actionType isEqualToString: LP_HELD_BACK_ACTION]) {
1865- // Spoof the message ID if this is a held back message.
1866- internalMessageId = [LP_HELD_BACK_MESSAGE_PREFIX stringByAppendingString: messageId];
1867- } else {
1868- internalMessageId = messageId;
1869- }
1852+ @synchronized (messages) {
1853+ NSMutableArray *actionContexts = [NSMutableArray array ];
18701854
1871- // Filter action types that don't match the filtering criteria.
1872- BOOL isForeground = ![actionType isEqualToString: LP_PUSH_NOTIFICATION_ACTION];
1873- if (isForeground) {
1874- if (!(filter & kLeanplumActionFilterForeground )) {
1855+ for (NSString *messageId in [messages allKeys ]) {
1856+ if (sourceMessage != nil && [messageId isEqualToString: sourceMessage]) {
18751857 continue ;
18761858 }
1877- } else {
1878- if (!(filter & kLeanplumActionFilterBackground )) {
1859+ NSDictionary *messageConfig = messages[messageId];
1860+ NSString *actionType = messageConfig[@" action" ];
1861+ if (![actionType isKindOfClass: NSString .class]) {
18791862 continue ;
18801863 }
1881- }
18821864
1883- LeanplumMessageMatchResult result = LeanplumMessageMatchResultMake (NO , NO , NO , NO );
1884- for (NSString *when in whenConditions) {
1885- LeanplumMessageMatchResult conditionResult =
1886- [[LPInternalState sharedState ].actionManager shouldShowMessage: internalMessageId
1887- withConfig: messageConfig
1888- when: when
1889- withEventName: eventName
1890- contextualValues: contextualValues];
1891- result.matchedTrigger |= conditionResult.matchedTrigger ;
1892- result.matchedUnlessTrigger |= conditionResult.matchedUnlessTrigger ;
1893- result.matchedLimit |= conditionResult.matchedLimit ;
1894- result.matchedActivePeriod |= conditionResult.matchedActivePeriod ;
1895- }
1865+ NSString *internalMessageId;
1866+ if ([actionType isEqualToString: LP_HELD_BACK_ACTION]) {
1867+ // Spoof the message ID if this is a held back message.
1868+ internalMessageId = [LP_HELD_BACK_MESSAGE_PREFIX stringByAppendingString: messageId];
1869+ } else {
1870+ internalMessageId = messageId;
1871+ }
18961872
1897- // Make sure it's within the active period.
1898- if (!result.matchedActivePeriod ) {
1899- continue ;
1900- }
1901-
1902- // Make sure we cancel before matching in case the criteria overlap.
1903- if (result.matchedUnlessTrigger ) {
1904- NSString *cancelActionName = [@" __Cancel" stringByAppendingString: actionType];
1905- LPActionContext *context = [LPActionContext actionContextWithName: cancelActionName
1906- args: @{}
1907- messageId: messageId];
1908- [self triggerAction: context handledBlock: ^(BOOL success) {
1909- if (success) {
1910- // Track cancel.
1911- [Leanplum track: @" Cancel" withValue: 0.0 andInfo: nil
1912- andArgs: @{LP_PARAM_MESSAGE_ID: messageId} andParameters: nil ];
1873+ // Filter action types that don't match the filtering criteria.
1874+ BOOL isForeground = ![actionType isEqualToString: LP_PUSH_NOTIFICATION_ACTION];
1875+ if (isForeground) {
1876+ if (!(filter & kLeanplumActionFilterForeground )) {
1877+ continue ;
1878+ }
1879+ } else {
1880+ if (!(filter & kLeanplumActionFilterBackground )) {
1881+ continue ;
19131882 }
1914- }];
1915- }
1916- if (result.matchedTrigger ) {
1917- [[LPInternalState sharedState ].actionManager recordMessageTrigger: internalMessageId];
1918- if (result.matchedLimit ) {
1919- NSNumber *priority = messageConfig[@" priority" ] ?: @(DEFAULT_PRIORITY);
1920- LPActionContext *context = [LPActionContext
1921- actionContextWithName: actionType
1922- args: [messageConfig objectForKey: LP_KEY_VARS]
1923- messageId: internalMessageId
1924- originalMessageId: messageId
1925- priority: priority];
1926- context.contextualValues = contextualValues;
1927- [actionContexts addObject: context];
19281883 }
1929- }
1930- }
19311884
1932- // Return if there are no action to trigger.
1933- if ([actionContexts count ] == 0 ) {
1934- return ;
1935- }
1885+ LeanplumMessageMatchResult result = LeanplumMessageMatchResultMake (NO , NO , NO , NO );
1886+ for (NSString *when in whenConditions) {
1887+ LeanplumMessageMatchResult conditionResult =
1888+ [[LPInternalState sharedState ].actionManager shouldShowMessage: internalMessageId
1889+ withConfig: messageConfig
1890+ when: when
1891+ withEventName: eventName
1892+ contextualValues: contextualValues];
1893+ result.matchedTrigger |= conditionResult.matchedTrigger ;
1894+ result.matchedUnlessTrigger |= conditionResult.matchedUnlessTrigger ;
1895+ result.matchedLimit |= conditionResult.matchedLimit ;
1896+ result.matchedActivePeriod |= conditionResult.matchedActivePeriod ;
1897+ }
19361898
1937- // Sort the action by priority and only show one message.
1938- // Make sure to capture the held back.
1939- [LPActionContext sortByPriority: actionContexts];
1940- NSNumber *priorityThreshold = [((LPActionContext *) [actionContexts firstObject ]) priority ];
1941- NSNumber *countDownThreshold = [self fetchCountDownForContext: (LPActionContext *) [actionContexts firstObject ] withMessages: messages];
1942- BOOL isPrioritySame = NO ;
1943- for (LPActionContext *actionContext in actionContexts) {
1944- NSNumber *priority = [actionContext priority ];
1945- if (priority.intValue > priorityThreshold.intValue ) {
1946- break ;
1899+ // Make sure it's within the active period.
1900+ if (!result.matchedActivePeriod ) {
1901+ continue ;
1902+ }
1903+
1904+ // Make sure we cancel before matching in case the criteria overlap.
1905+ if (result.matchedUnlessTrigger ) {
1906+ NSString *cancelActionName = [@" __Cancel" stringByAppendingString: actionType];
1907+ LPActionContext *context = [LPActionContext actionContextWithName: cancelActionName
1908+ args: @{}
1909+ messageId: messageId];
1910+ [self triggerAction: context handledBlock: ^(BOOL success) {
1911+ if (success) {
1912+ // Track cancel.
1913+ [Leanplum track: @" Cancel" withValue: 0.0 andInfo: nil
1914+ andArgs: @{LP_PARAM_MESSAGE_ID: messageId} andParameters: nil ];
1915+ }
1916+ }];
1917+ }
1918+ if (result.matchedTrigger ) {
1919+ [[LPInternalState sharedState ].actionManager recordMessageTrigger: internalMessageId];
1920+ if (result.matchedLimit ) {
1921+ NSNumber *priority = messageConfig[@" priority" ] ?: @(DEFAULT_PRIORITY);
1922+ LPActionContext *context = [LPActionContext
1923+ actionContextWithName: actionType
1924+ args: [messageConfig objectForKey: LP_KEY_VARS]
1925+ messageId: internalMessageId
1926+ originalMessageId: messageId
1927+ priority: priority];
1928+ context.contextualValues = contextualValues;
1929+ [actionContexts addObject: context];
1930+ }
1931+ }
1932+ }
1933+
1934+ // Return if there are no action to trigger.
1935+ if ([actionContexts count ] == 0 ) {
1936+ return ;
19471937 }
1948- if (isPrioritySame) {// priority is same
1949- NSNumber *currentCountDown = [self fetchCountDownForContext: actionContext withMessages: messages];
1950- // multiple messages have same priority and same countDown, only display one message
1951- if (currentCountDown == countDownThreshold) {
1938+
1939+ // Sort the action by priority and only show one message.
1940+ // Make sure to capture the held back.
1941+ [LPActionContext sortByPriority: actionContexts];
1942+ NSNumber *priorityThreshold = [((LPActionContext *) [actionContexts firstObject ]) priority ];
1943+ NSNumber *countDownThreshold = [self fetchCountDownForContext: (LPActionContext *) [actionContexts firstObject ] withMessages: messages];
1944+ BOOL isPrioritySame = NO ;
1945+ for (LPActionContext *actionContext in actionContexts) {
1946+ NSNumber *priority = [actionContext priority ];
1947+ if (priority.intValue > priorityThreshold.intValue ) {
19521948 break ;
19531949 }
1954- }
1955- isPrioritySame = YES ;
1956- if ([[actionContext actionName ] isEqualToString: LP_HELD_BACK_ACTION]) {
1957- [[LPInternalState sharedState ].actionManager
1950+ if (isPrioritySame) {// priority is same
1951+ NSNumber *currentCountDown = [self fetchCountDownForContext: actionContext withMessages: messages];
1952+ // multiple messages have same priority and same countDown, only display one message
1953+ if (currentCountDown == countDownThreshold) {
1954+ break ;
1955+ }
1956+ }
1957+ isPrioritySame = YES ;
1958+ if ([[actionContext actionName ] isEqualToString: LP_HELD_BACK_ACTION]) {
1959+ [[LPInternalState sharedState ].actionManager
19581960 recordHeldBackImpression: [actionContext messageId ]
1959- originalMessageId: [actionContext originalMessageId ]];
1960- } else {
1961- [self triggerAction: actionContext handledBlock: ^(BOOL success) {
1962- if (success) {
1963- [[LPInternalState sharedState ].actionManager
1961+ originalMessageId: [actionContext originalMessageId ]];
1962+ } else {
1963+ [self triggerAction: actionContext handledBlock: ^(BOOL success) {
1964+ if (success) {
1965+ [[LPInternalState sharedState ].actionManager
19641966 recordMessageImpression: [actionContext messageId ]];
1965- }
1966- }];
1967+ }
1968+ }];
1969+ }
19671970 }
19681971 }
19691972}
0 commit comments