- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
将此作为问题发布到 AFNetworking 存储库后,事实证明这实际上是我的使用问题。根据对我的问题的回复:
NSURLSession retains its delegate (i.e. AFURLSessionManager). Call invalidateSessionCancelingTasks: to ensure that sessions finalize and release their delegate.
所以,长话短说:如果您以下面描述的方式使用 AHTTPSessionManager
,请确保调用 invalidateSessionCancelingTasks:
以确保 session 完成并释放它们的委托(delegate)
我有一个名为 GTAPIClient
的子类 AFHTTPSessionManager
,我用它来连接到我的 REST API。我意识到文档状态可以用作单例,但在某些情况下我需要创建一个新实例。但是,似乎每当我这样做时,该对象就永远不会被释放。目前,GTAPIClient
在释放时除了 NSLog
本身外什么都不做。这是一些演示行为的示例代码
@implementation GTAPIClient
- (void)dealloc
{
NSLog(@"Dealloc: %@", self);
}
@end
#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
启动 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)
触发 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, baseURL: (null), session: <__NSCFURLSession: 0x10b915010>, operationQueue: <NSOperationQueue: 0x10b9148a0>{name = 'NSOperationQueue 0x10b9148a0'}>
2014-06-22 16:44:43.723 Entity: (null)
作为引用,我使用的是 AFNetworking v2.3.1。编译器警告我,将保留的对象分配给 weak 属性将在分配后释放 - 这是正确的,并且对我的随机对象具有预期的功能。应用程序中没有其他任何事情发生。没有其他 View Controller ,GTAPIClient
上没有其他方法,所有单例功能都被删除。对我在这里做错了什么有什么想法吗?
最佳答案
在此处发布 Mattt Thompson 的回复以帮助 future 的读者:
NSURLSession
retains its delegate (i.e.AFURLSessionManager
). CallinvalidateSessionCancelingTasks:
to ensure that sessions finalize and release their delegate.
如果像许多应用一样,您的应用对整个应用使用单例 session 管理器和一个 URL session ,那么您无需担心这一点。
关于objective-c - AFHTTPSessionManager 未解除分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355635/
我在我的表格 View 中应用了这段代码。但是,我意识到第一次请求时数据加载速度会很慢。之后,如果我访问同一页面,记录将加载得更快。有什么改进第一次加载的想法吗?感谢您的帮助。 AFHTTPSessi
我正在使用 Afnetworking Framework,但每次我点击网络服务时,响应总是如下所示- Error Domain=com.alamofire.error.serialization.re
我正在尝试通过设置 HTTPAdditionalHeaders 为“Content-Type”设置默认 header 。当我查看请求 header 时,AFNetworking (v 2.0.3) 将
我正在使用 OAuth2 身份验证开发 iOS 应用程序。 我已通过身份验证,请求我的 API 没有问题,但在尝试从 protected URLS 下载某些文件时遇到问题。 我正在使用 downloa
我的应用程序使用 AFHTTPSessionManager 的子类下载信息和获取。用户可能会选择退出下载,我正试图弄清楚如何取消下载。 我遇到了这段代码: - (void) cancel {
我正在尝试使用 AFNetworking2.0 和新的 NSURLSession 类来获取一些数据。特别是我对 NSURLSessionConfiguration 的新 HTTPMaximumConn
由于我是 IOS 的新手,AFNetworking 3,0 是新的,所以我不知道如何从 AFHTTPSessionManager 中检索数据。我必须关注消息,我想返回结果 - (NSString *)
使用自定义 session 配置创建 AFHTTPSessionManager 单例子类的正确方法是什么? class DefaultSessionConfiguration: NSURLSessio
我已经坚持了一段时间,我正在使用 Youtube 的 api,但我一直收到 403 错误。我正在使用负责 OAuth2 的 Google 登录 NSString *authToken = [[NSS
我有一个文本字段,每当用户输入字符时,我都会将该字符附加到我的 url 并获得响应。 我需要取消之前的请求。例如,如果我输入 “shampoo”,我将访问服务器 7 次,我需要取消最后 6 次请求。目
我有一个应用程序,我在其中使用 session 管理器调用同步请求,在 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY
对于 AFNetworking,我遵循了创建 AFHTTPSessionManager 的应用程序特定子类的建议模式。我的看起来像这样: static NSString* Username = @""
更新 将此作为问题发布到 AFNetworking 存储库后,事实证明这实际上是我的使用问题。根据对我的问题的回复: NSURLSession retains its delegate (i.e. A
我也需要向我的服务器发送一个 post 请求。 AFHTTPRequestOperation 非常简单,只需使用: [request setHTTPBody: [requestBody dataUsi
我正在尝试使用 AFNetworking 2.0 来执行我的网络请求,但我遇到了一些奇怪的行为。我已经子类化 AFHTTPSessionManager如 suggested in the docume
我检查了 Stackoverflow 中的几篇文章(this 和 this),但没有一篇回答“如何使用 AFHTTPSessionManager 来使用基于 SOAP 的 Web 服务”。 这是不可能
我正在使用新的 afnetworking 3.0 库发出网络请求以使用 AFHTTPSessionmanager 进行登录。因此,当我执行 POST 调用并在响应中收到 401 时,会静默调用另一个网
我的应用得到静默推送,然后使用 AFNetworking 在后台做一些 http 请求,但它没有进入完整的 block ,代码是: AFHTTPSessionManager *manager = [[
当我使用 AFHTTPRequestOperationManager 时,我可以将 HTTPRequestOperationWithRequest 方法与 NSURLRequest 对象一起使用。有了
我最近遇到了一个问题,我们的一位客户的互联网连接速度非常慢,而且还使用 GroundControl。每当他们尝试加载我的应用程序时,他们都会卡在“Attemping to Connect to Ser
我是一名优秀的程序员,十分优秀!