From 5805330931e2193972053f15e5786d68f86c4b8f Mon Sep 17 00:00:00 2001 From: wqyfavor Date: Tue, 23 Apr 2019 14:48:50 +0800 Subject: [PATCH] [iOS] Monitor "Too many timers" error. And remove invalidate timers automatically. (#2352) --- ios/sdk/WeexSDK/Sources/Engine/WXSDKError.h | 4 +++- ios/sdk/WeexSDK/Sources/Engine/WXSDKError.m | 4 +++- .../WeexSDK/Sources/Module/WXTimerModule.m | 20 +++++++++++++++++++ .../Sources/Monitor/WXExceptionUtils.h | 1 + ios/sdk/WeexSDK/Sources/Utility/WXVersion.m | 4 ++-- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.h b/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.h index 52a9f20710..99b1f60a98 100644 --- a/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.h +++ b/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.h @@ -82,7 +82,9 @@ typedef NS_ENUM(int, WXSDKErrCode) WX_KEY_EXCEPTION_ABILITY_DOWN_ = -9603, WX_KEY_EXCEPTION_EMPTY_SCREEN_JS = -9700, - WX_KEY_EXCEPTION_EMPTY_SCREEN_NATIVE = -9701 + WX_KEY_EXCEPTION_EMPTY_SCREEN_NATIVE = -9701, + + WX_KEY_EXCEPTION_TOO_MANY_TIMERS = -9800, }; typedef NS_ENUM (NSInteger,WXSDKErrorType) diff --git a/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.m b/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.m index 36aaa98288..5199bf29b7 100644 --- a/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.m +++ b/ios/sdk/WeexSDK/Sources/Engine/WXSDKError.m @@ -101,7 +101,9 @@ +(NSDictionary *) getMap @(WX_ERR_JSFRAMEWORK_START):@{ERROR_TYPE:@(WX_NATIVE_ERROR),ERROR_GROUP:@(WX_NATIVE)}, @(WX_KEY_EXCEPTION_ABILITY_DOWN_):@{ERROR_TYPE:@(WX_NATIVE_ERROR),ERROR_GROUP:@(WX_NATIVE)}, @(WX_KEY_EXCEPTION_EMPTY_SCREEN_JS):@{ERROR_TYPE:@(WX_RENDER_ERROR),ERROR_GROUP:@(WX_JS)}, - @(WX_KEY_EXCEPTION_EMPTY_SCREEN_NATIVE):@{ERROR_TYPE:@(WX_RENDER_ERROR),ERROR_GROUP:@(WX_NATIVE)} + @(WX_KEY_EXCEPTION_EMPTY_SCREEN_NATIVE):@{ERROR_TYPE:@(WX_RENDER_ERROR),ERROR_GROUP:@(WX_NATIVE)}, + + @(WX_KEY_EXCEPTION_TOO_MANY_TIMERS):@{ERROR_TYPE:@(WX_NATIVE_ERROR),ERROR_GROUP:@(WX_NATIVE)} }; }); return codeMap; diff --git a/ios/sdk/WeexSDK/Sources/Module/WXTimerModule.m b/ios/sdk/WeexSDK/Sources/Module/WXTimerModule.m index cc399ac533..d1dc4ef8bc 100644 --- a/ios/sdk/WeexSDK/Sources/Module/WXTimerModule.m +++ b/ios/sdk/WeexSDK/Sources/Module/WXTimerModule.m @@ -23,6 +23,8 @@ #import "WXAssert.h" #import "WXMonitor.h" #import "WXSDKInstance_performance.h" +#import "WXSDKError.h" +#import "WXExceptionUtils.h" @interface WXTimerTarget : NSObject @@ -75,6 +77,7 @@ + (void) checkExcuteInBack:(NSString*) instanceId @implementation WXTimerModule { + BOOL _tooManyTimersReported; NSMutableDictionary *_timers; } @@ -164,6 +167,23 @@ - (void)createTimerWithCallback:(NSString *)callbackID time:(NSTimeInterval)mill if (!_timers[callbackID]) { _timers[callbackID] = timer; + + if ([_timers count] > 30) { + if (!_tooManyTimersReported) { + [WXExceptionUtils commitCriticalExceptionRT:self.weexInstance.instanceId errCode:[NSString stringWithFormat:@"%d", WX_KEY_EXCEPTION_TOO_MANY_TIMERS] function:@"" exception:@"Too many timers." extParams:nil]; + _tooManyTimersReported = YES; + } + + // remove invalid timers + NSMutableArray* invalidTimerIds = [[NSMutableArray alloc] init]; + for (NSString *cbId in _timers) { + NSTimer *timer = _timers[cbId]; + if (![timer isValid]) { + [invalidTimerIds addObject:cbId]; + } + } + [_timers removeObjectsForKeys:invalidTimerIds]; + } } } diff --git a/ios/sdk/WeexSDK/Sources/Monitor/WXExceptionUtils.h b/ios/sdk/WeexSDK/Sources/Monitor/WXExceptionUtils.h index c0bcf4b89b..6bd13e0904 100644 --- a/ios/sdk/WeexSDK/Sources/Monitor/WXExceptionUtils.h +++ b/ios/sdk/WeexSDK/Sources/Monitor/WXExceptionUtils.h @@ -25,5 +25,6 @@ + (void)commitCriticalExceptionRT:(NSString *)instanceId errCode:(NSString *)errCode function:(NSString *)function exception:(NSString *)exception extParams:(NSDictionary *)extParams; + (void)commitCriticalExceptionRT:(WXJSExceptionInfo*)jsExceptionInfo; + @end diff --git a/ios/sdk/WeexSDK/Sources/Utility/WXVersion.m b/ios/sdk/WeexSDK/Sources/Utility/WXVersion.m index 033df66a15..6739353498 100644 --- a/ios/sdk/WeexSDK/Sources/Utility/WXVersion.m +++ b/ios/sdk/WeexSDK/Sources/Utility/WXVersion.m @@ -20,8 +20,8 @@ #import "WXVersion.h" #import "WXDefine.h" -static const char* WeexSDKBuildTime = "2019-04-16 13:26:54 UTC"; -static const unsigned long WeexSDKBuildTimestamp = 1555421214; +static const char* WeexSDKBuildTime = "2019-04-23 06:34:41 UTC"; +static const unsigned long WeexSDKBuildTimestamp = 1556001281; NSString* GetWeexSDKVersion(void) {