gpt4 book ai didi

ios - AFNetworking 代码给我内存泄漏

转载 作者:行者123 更新时间:2023-11-29 12:26:16 25 4
gpt4 key购买 nike

@implementation GetData

static NSString *string = @"https://afternoon-springs-7986.herokuapp.com/";
static NSString *baseStr = @"https://afternoon-springs-7986.herokuapp.com/updateInformation";

-(void) postEventInfo: (NSDictionary *) eventInfoObject


{
NSURL *url = [NSURL URLWithString:string]; // 6.5%
// NSURL *baseURL = [NSURL URLWithString:@"http://localhost:5000/"];

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;

self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%

[self.manager POST:@"/addEvent/" parameters:eventInfoObject success:^(NSURLSessionDataTask *task, id responseObject) { // 12.9%

[FVCustomAlertView showDefaultDoneAlertOnView:topView withTitle:@"Klart!"];

} failure:^(NSURLSessionDataTask *task, NSError *error) {

[FVCustomAlertView showDefaultErrorAlertOnView:topView withTitle:@"Ett fel uppstod, försök igen!"];
}];
}

我在上述代码中收到内存泄漏。如您所见,我评论了 % 的数量,与 Leaks 工具所做的相同。我正在运行 Xcode 6,测试是在我的 iPhone 设备 5s IOS 7.1.1 上进行的

这是泄漏工具的截图。 https://www.dropbox.com/s/beh4no79wgk54bm/Screen%20Shot%202015-03-12%20at%2013.09.53.png?dl=0

最佳答案

每次调用“postEventInfo”时,您都在创建一个AFHTTPSessionManager 对象。

如果您使用的是 ARC,这应该意味着旧对象被释放(即不是这样的问题)。但为了最佳实践,你应该这样做:

// set self.manager only if it hasn't been created yet
if(!self.manager)
{
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%
}

关于ios - AFNetworking 代码给我内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29009469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com