更新
在将此问题作为问题发布到AFNetworking回购后,事实证明这实际上是我的使用问题.根据我的问题回复:
NSURLSession retains its delegate (i.e. AFURLSessionManager). Call invalidateSessionCancelingTasks: to ensure that sessions finalize and release their delegate.
所以,长话短说:如果你以下面描述的方式使用AHTTPSessionManager,请确保调用invalidateSessionCancelingTasks:以确保会话最终确定并释放其委托
原始问题
我有一个名为GTAPIClient的子类AFHTTPSessionManager,我用它连接到我的REST API.我意识到文档声明要用作单例,但在某些情况下我需要编写一个新实例.但是,似乎每当我这样做时,对象永远不会被释放.目前,GTAPIClient在解除分配时除了NSLog本身之外什么都不做.
这是一些演示行为的示例代码
GTAPIClient.m
@implementation GTAPIClient - (void)dealloc { NSLog(@"Dealloc: %@",self); } @end
GTViewController.m
#import "GTBaseEntityViewController.h" //Models #import "GTBaseEntity.h" //Clients #import "GTAPIClient.h" @interface GTBaseEntityViewController () @property (nonatomic,weak) GTAPIClient *client; @property (nonatomic,weak) GTBaseEntity *weakEntity; @end @implementation GTBaseEntityViewController - (IBAction)makeClient:(id)sender { self.client = [[GTAPIClient alloc] init]; NSLog(@"I just made an API client %@",self.client); //Another random object assigned to a similar property,just to see what happens. self.weakEntity = [[GTBaseEntity alloc] init]; NSLog(@"I just made a random object %@",self.weakEntity); } - (IBAction)checkClient:(id)sender { NSLog(@"Client: %@",self.client); NSLog(@"Entity: %@",self.weakEntity); } @end
NSLog输出
Fire makeClient:
//It seems to me that both NSLog's should return (null) as they are assigning to a weak property 2014-06-22 16:41:39.143 I just made an API client <GTAPIClient: 0x10b913680,baseURL: (null),session: <__NSCFURLSession: 0x10b915010>,operationQueue: <NSOperationQueue: 0x10b9148a0>{name = 'NSOperationQueue 0x10b9148a0'}> 2014-06-22 16:41:39.144 I just made a random object (null)
Fire checkClient
//Again,both NSLog's should return null for the two objects. However...client is still around. Also,it's overridden dealloc method never fired. 2014-06-22 16:44:43.722 Client: <GTAPIClient: 0x10b913680,operationQueue: <NSOperationQueue: 0x10b9148a0>{name = 'NSOperationQueue 0x10b9148a0'}> 2014-06-22 16:44:43.723 Entity: (null)
作为参考,我正在使用AFNetworking的v2.3.1.编译器警告我,将保留对象分配给weak属性将在赋值后释放 – 这是正确的,并且与我的随机对象一起运行.应用程序中没有其他任何内容.没有其他视图控制器,GTAPIClient上没有其他方法,所有单例功能都被删除.对我在这里做错了什么的想法?
解决方法
复制场景并通过Instruments运行它表明AFURLSessionManagers由它们创建的NSURLSession保留,因为AFURLSessionManager充当每个创建的NSURLSession的委托.这会创建一个保留周期,因此无法释放AFHTTPSessionManager.无论这是一个库中的错误还是根本没有错误,我都不确定.您可能希望在AFNetworking GitHub页面(
https://github.com/AFNetworking/AFNetworking)上报告它.