Giter VIP home page Giter VIP logo

Comments (9)

hcl416029105 avatar hcl416029105 commented on May 18, 2024

image

from ios-monitor-platform.

aozhimin avatar aozhimin commented on May 18, 2024

@hcl416029105 我先看看,晚上给你答复。

from ios-monitor-platform.

hcl416029105 avatar hcl416029105 commented on May 18, 2024

如果不用AFHTTPSessionManager,直接用原生的NSURLSession,没有问题:
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];

    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://hcz-static.pingan.com.cn/fuelCard/fuelCard.zip"]];
    
    [task resume];

这种可以正常调用。

好的,非常感谢你的解答

from ios-monitor-platform.

aozhimin avatar aozhimin commented on May 18, 2024

@hcl416029105 多谢提供的信息,我这边看了下,用 AFNetworking 出问题的原因是在 CFNetwork -[__NSCFURLLocalSessionConnection initWithTask:delegate:delegateQueue:] 方法中会调用 __NSURLSessionLocalcan_delegate_task_willSendRequestForEstablishedConnection 方法,而这个方法中会执行下列方法:

[delegate respondsToSelector:@selector(URLSession:task:_willSendRequestForEstablishedConnection:completionHandler:)]

delegate 此时已经是你的 NSURLDelegateProxy 对象,他无法响应 respondsToSelector,所以会走消息转发的流程中,也就会走到 NSURLDelegateProxy 对象 的 methodSignatureForSelector

- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    
    NSMethodSignature *methodSignature = nil;
    
    if ( [self.proxyTarget respondsToSelector:sel]) {
        methodSignature = [self.proxyTarget methodSignatureForSelector:sel];
    }
    
    return methodSignature;
}

但是因为你的 proxyTargetinitWithTarget 被赋值为 AFHTTPSessionManager 对象,而且这个属性为弱引用,而执行到 methodSignatureForSelector 时,AFHTTPSessionManager 对象已经被释放,所以这里 return 的 methodSignature 实际为 nil,导致消息转发流程失败,也就有了下面的输出:

*** NSForwarding: warning: object %p of class '%s' does not implement doesNotRecognizeSelector: -- abort

将属性改为 strong,发现问题已经没有了。

@property(nonatomic, strong) id proxyTarget;

当然你可能会问为什么直接使用 NSURLSession 的 API 无此问题。

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
    
    NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://hcz-static.pingan.com.cn/fuelCard/fuelCard.zip"]];
    
    [task resume];

事实上使用 NSURLSession 的 API 和 AF 在 CFNetwork 层都会走上面的那段逻辑,但是由于sessionWithConfiguration:delegate:delegateQueue 方法的入参 delegate 恰好是一个强引用的对象,比如我这里可能是一个 ViewController,所以即使 proxy 那边是弱引用,执行到 methodSignatureForSelector 能够正常返回方法签名对象,消息转发的流程得以正常执行,所以这种场景无此问题。

具体你可以在自己项目中调试来验证,建议在 NSURLDelegateProxymethodSignatureForSelector 方法下断点来验证。

from ios-monitor-platform.

aozhimin avatar aozhimin commented on May 18, 2024

@hcl416029105 另外关于网络监控这块可以看下我的另外一篇文章揭秘 APM iOS SDK 的核心技术,文章对听云的网络监控实现进行了一些探索,希望能对你有所帮助。

from ios-monitor-platform.

hcl416029105 avatar hcl416029105 commented on May 18, 2024

网络监控搞复杂了,不用各种swizze,各种hook,直接取getifaddrs获取的数据

from ios-monitor-platform.

aozhimin avatar aozhimin commented on May 18, 2024

@hcl416029105 getifaddrs() 函数拿到的 ifaddrs struct 最多也是本地地址的信息吧,类似 Local IP 这些,网络监控要获取到的远远不止这个。

from ios-monitor-platform.

MoShenGuo avatar MoShenGuo commented on May 18, 2024

image
如果强引用的话 会不会引起对象释放不了的问题吗

from ios-monitor-platform.

karosLi avatar karosLi commented on May 18, 2024

image
如果强引用的话 会不会引起对象释放不了的问题吗

同问

from ios-monitor-platform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.