我有一个测试应用程序,我在这个应用程序中的内容是通过PHP脚本进行的调用,一旦数据返回,再次进行递归调用再次调用PHP脚本等等:
每次发生的事情都是[Self recusiveForumActivity];被称为我获得300kb的内存分配,并在调用此递归方法时内存使用率不断攀升。如果我删除该方法,内存使用率保持稳定。我怎样才能克服这个问题,以便在每次调用递归方法时都不会松掉内存分配?
这是代码:
//
// ViewController.m
// Test
//
// Created by trikam patel on 30/06/2015.
// Copyright (c) 2015 trikam patel. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSString*)setupPhpCall:(NSString*)requestString :(NSString*)sciptPage{
//NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:0];
//[NSURLCache setSharedURLCache:sharedCache];
NSHTTPURLResponse *urlresponse = nil;
NSError *error = nil;
NSString *response = @"";
NSData *myRequestData = nil;
NSMutableURLRequest *request = nil;
NSData *returnData = nil;
myRequestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
//Create your request string with parameter name as defined in PHP file
request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://www.hugt.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 = nil;
//if(tmpArray.count == 0){
returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlresponse error: &error];
response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
//}
// Log Response
urlresponse = nil;
error = nil;
myRequestData = nil;
request = nil;
returnData = nil;
//NSLog(@"%@",response);/****/
//[sharedCache removeAllCachedResponses];
if(response != nil){
return response;
}
return nil;
}
-(void)recurseForumActivity{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
__block NSString *myRequestStringForum = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",@"",@"", @"", @"", @""];
__block NSString *responseForum = [self setupPhpCall:myRequestStringForum :@"getThreadRecurse.php"];
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:2.0f];
responseForum = @"";
myRequestStringForum = @"";
[self recurseForumActivity];
});
});
});
});
}
- (void)viewDidLoad {
[super viewDidLoad];
[self recurseForumActivity];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:3)
尝试使用[object performSelector:method]
而不是递归调用。递归函数必须始终具有基本/退出条件,并且您的代码不具有任何此类条件。所以你不能在这里进行任何递归调用。
- (void)recurseForumActivity{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
__block NSString *myRequestStringForum = [NSString stringWithFormat:@"lastDate=%@&threadTitle=%@&threadCountry=%@&threadCategory=%@&threadSubCategory=%@&getData=0",@"",@"", @"", @"", @""];
__block NSString *responseForum = [self setupPhpCall:myRequestStringForum :@"getThreadRecurse.php"];
YourClass * __weak weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
responseForum = @"";
myRequestStringForum = @"";
[weakSelf performSelector:@selector(recurseForumActivity) withObject:nil afterDelay:2.0f];
});
});
});
});
}