Authored by 孙凯

解决 crash review by hongmo

... ... @@ -16,11 +16,35 @@
@interface NSURLConnection()
@property(nullable,nonatomic,weak) id my_delegate;
@property (readonly, copy) NSURLResponse *my_response;
@end
@implementation NSURLConnection (AutoTrack)
#pragma mark - 属性
- (id)my_delegate
{
return objc_getAssociatedObject(self, _cmd);
}
- (void)setMy_delegate:(id)my_delegate
{
objc_setAssociatedObject(self, @selector(my_delegate), my_delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)my_response
{
return objc_getAssociatedObject(self, _cmd);
}
- (void)setMy_response:(id)my_response
{
objc_setAssociatedObject(self, @selector(my_response), my_response, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+(void)startTrack{
static dispatch_once_t onceToken;
... ... @@ -76,16 +100,40 @@
- (nullable instancetype)yher_initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate startImmediately:(BOOL)startImmediately
{
return [self yher_initWithRequest:request delegate:delegate startImmediately:startImmediately];
[self setMy_delegate:delegate];
return [self yher_initWithRequest:request delegate:self startImmediately:startImmediately];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector])
return YES;
if ([self.my_delegate respondsToSelector:aSelector])
return YES;
return NO;
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
if ([self.my_delegate respondsToSelector:aSelector])
return self.my_delegate;
return [super forwardingTargetForSelector:aSelector];
}
- (nullable instancetype)yher_initWithRequest:(NSURLRequest *)request delegate:(nullable id)delegate
{
return [self yher_initWithRequest:request delegate:delegate];
[self setMy_delegate:delegate];
return [self yher_initWithRequest:request delegate:self];
}
- (void)yher_start
{
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:self.currentRequest];
}
[self yher_start];
}
... ... @@ -94,7 +142,7 @@
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventStartWithRequest:request];
}
NSData *data = [self yher_sendSynchronousRequest:request returningResponse:response error:error];
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
... ... @@ -122,4 +170,38 @@
}];
}
#pragma mark - NSURLConnectionDelegate
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
connection.my_response = response;
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didReceiveResponse:)]) {
[self.my_delegate connection:connection didReceiveResponse:response];
}
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didReceiveData:)]) {
[self.my_delegate connection:connection didReceiveData:data];
}
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connectionDidFinishLoading:)]) {
[self.my_delegate connectionDidFinishLoading:connection];
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:connection.currentRequest response:connection.my_response];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (self.my_delegate && [self.my_delegate respondsToSelector:@selector(connection:didFailWithError:)]) {
[self.my_delegate connection:connection didFailWithError:error];
}
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].httpPerformanceTrackEnable) {
[[YH_EventCollector sharedInstance] timeEventEndWithRequest:connection.currentRequest response:nil];
}
}
@end
... ...
... ... @@ -137,7 +137,7 @@
{
NSString *selectorAction = NSStringFromSelector(action);
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]) {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && [self isKindOfClass:[UIButton class]]&&![selectorAction isEqualToString:@"backAction:"] && [target respondsToSelector:action]) {
if ([[selectorAction componentsSeparatedByString:@":"]count]==1) {
[UIControl yher_exchangeSelector:action target:target toSelector:@selector(yher_UIControl_didTouch_cmd)];
... ...
... ... @@ -58,7 +58,7 @@
{
NSString *selectorAction = NSStringFromSelector(action);
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && ([target isKindOfClass:[UIImageView class]] || [target isKindOfClass:[UILabel class]])) {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && ([target isKindOfClass:[UIImageView class]] || [target isKindOfClass:[UILabel class]])&& ![selectorAction isEqualToString:@"beenTapped:"]) {
if ([[selectorAction componentsSeparatedByString:@":"]count]==2) {
[UITapGestureRecognizer yher_exchangeSelector:action target:target toSelector:@selector(yher_gestureRecognizerDidTouch_cmd:)];
}
... ... @@ -70,7 +70,7 @@
{
NSString *selectorAction = NSStringFromSelector(action);
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && ([target isKindOfClass:[UIImageView class]] || [target isKindOfClass:[UILabel class]])) {
if ([YHEventReport sharedInstance].isPerformanceTrackEnabled && [YHEventReport sharedInstance].buttonPerformanceTrackEnable && ([target isKindOfClass:[UIImageView class]] || [target isKindOfClass:[UILabel class]])&& ![selectorAction isEqualToString:@"beenTapped:"]) {
if ([[selectorAction componentsSeparatedByString:@":"]count]==2) {
[UITapGestureRecognizer yher_exchangeSelector:action target:target toSelector:@selector(yher_gestureRecognizerDidTouch_cmd:)];
}
... ...
... ... @@ -314,6 +314,10 @@
#pragma mark - UIControl performance
- (void)timeEventStartWithUIControl:(UIControl *)control
{
if (!control) {
return;
}
NSNumber *startTime = @([[NSDate date] timeIntervalSince1970]);
NSString *viewId = [NSString stringWithFormat:@"%@%@",NSStringFromClass([control class]),startTime];
... ... @@ -321,9 +325,7 @@
return;
}
if (!control) {
return;
}
control.yh_viewId=viewId;
dispatch_async(self.serialQueue, ^{
... ... @@ -333,6 +335,10 @@
- (void)timeEventEndWithUIControl:(UIControl *)control
{
if (!control) {
return;
}
NSTimeInterval elapsedTime = [self eventElapsedTime:control.yh_viewId];
if (elapsedTime==0) {
... ...