gpt4 book ai didi

IOS - 递归函数离开内存分配

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:37 28 4
gpt4 key购买 nike

我有一个测试应用程序,我在这个应用程序中拥有的是通过 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

最佳答案

尝试使用 [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];
});
});

});

});
}

关于IOS - 递归函数离开内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31149404/

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