我有一个使用NSTimer的递归循环,它每两秒运行一次并调用一个调用setupPhpCall函数的函数:
有3个NSTimers运行并始终点击setupPhpCall函数, 这可能是内存泄漏发生的地方吗?
-(NSString*)setupPhpCall:(NSString*)requestString :(NSString*)sciptPage{
NSData *myRequestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
// Create your request string with parameter name as defined in PHP file
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://www.xxxx.co.uk/%@", sciptPage]]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&urlResponse error: &error];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
return response;
}
这是使用NSTimer调用的递归方法:
-(void)recurseForumActivity:(NSTimer *)timer{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
NSString *myRequestString = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",lastDateForumActivity,searchThreadTitle, searchThreadCountry, searchThreadCategory, searchThreadSubCategory];
NSString *response = [self setupPhpCall:myRequestString :@"xxx.php"];
dispatch_async(dispatch_get_main_queue(), ^{
if(response.length > 0 && ![response isEqualToString:@"[]"]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%@ new threads...", response];
if(imageviewForumAlert == NULL){
UIImage *image = [UIImage imageNamed:@"alert.png"];
imageviewForumAlert = [UIImageView new];
[viewNav1 addSubview:imageviewForumAlert];
imageviewForumAlert.translatesAutoresizingMaskIntoConstraints = NO;
imageviewForumAlert.image = image;
NSDictionary *viewsDictionary = @{@"imageviewForumAlert":imageviewForumAlert};
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-19-[imageviewForumAlert(12)]-19-|"
options:0
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraint_H];
[self.view addConstraints:constraint_V];
}else{
imageviewForumAlert.hidden = NO;
}
/**NSDictionary *dic = [response JSONValue];
if((NSNull*)dic != [NSNull null]){
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", dic.count];
}**/
}else{
imageviewForumAlert.hidden = YES;
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}
/**else{
labelNewForumThreads.text = [NSString stringWithFormat:@"%d new threads...", 0];
}**/
});
});
}
在我离开应用程序运行超过一小时后,在控制台中出现此错误:
Communications error: <OS_xpc_error: <error: 0x361c0614> { count = 1, contents =
"XPCErrorDescription" => <string: 0x361c086c> { length = 22, contents = "Connection interrupted" }
}>
我有运行aswel的分配工具
这是应用崩溃时分配工具的屏幕截图
我也注意到在启动乐器分配工具时我的内存使用率会快速上升,为什么会这样?