Iphone iOS中的调度\u异步内存问题
我有一个大约2000多个项目的循环,我需要通过:Iphone iOS中的调度\u异步内存问题,iphone,ios,grand-central-dispatch,Iphone,Ios,Grand Central Dispatch,我有一个大约2000多个项目的循环,我需要通过: for (NSDictionary* dict in array) { NSString *fileName = [NSString stringWithFormat:@"%@/%@_lg.jpg?t=",manufacturerID, [[dict valueForKey:@"ItemID"] stringByReplacingOccurrencesOfString:@" " withString:@"%20
for (NSDictionary* dict in array) {
NSString *fileName = [NSString stringWithFormat:@"%@/%@_lg.jpg?t=",manufacturerID, [[dict valueForKey:@"ItemID"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"]];
NSString *savePath = [documentsPath stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL URLWithString: [[NSString stringWithFormat:kProductImagesURL, fileName]stringByAppendingString:lastImagesSyncDate]];
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(aQueue, ^{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
int statusCode = [request responseStatusCode];
if (statusCode==200) {
NSData *responseData = [request responseData];
[responseData writeToFile:[savePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "] atomically:YES];
}
}
});
}
这很好用,我的主线程没有被阻塞,但是我的记忆已经崩溃了——我怎样才能让它被释放呢?一旦队列是空的,它就会下降,但我需要它在前进时清空
ASIHTTPRequest
,而是尝试alloc、init和release李>
ASIHTTPRequest
,而是尝试alloc、init和release李>
虽然您在后台运行代码,但同时在后台运行所有代码。您可以尽可能快地在阵列中循环,创建一个新的ASIHttpRequest,该ASIHttpRequest将同时尝试下载和保存数据。您可能希望将循环移动到
调度\u async
中,或者使用执行相同操作但限制上最大并发操作数的。如果将循环移动到dispatch\u async
中,一次执行一个循环,请记住在本地创建一个循环,并定期将其排空。虽然在后台运行代码,但同时在后台运行所有代码。您可以尽可能快地在阵列中循环,创建一个新的ASIHttpRequest,该ASIHttpRequest将同时尝试下载和保存数据。您可能希望将循环移动到调度\u async
中,或者使用执行相同操作但限制上最大并发操作数的。如果将循环移动到dispatch#u async
中,一次执行一个循环,请记住在本地创建一个循环,并定期将其排空。或使用信号量来控制同步操作。我最终只是在ASIHTTPRequest位周围放置了一个NSAutoReleasePool,它起到了很好的作用-我的部分问题是我没有构建它每次都是为了探查器,所以我都在进行更改,却看不出有什么不同——伪造者使用信号量来控制同步操作,这是一件令人沮丧的蠢事。最后,我只是在ASIHTTPRequest位周围放置了一个NSAutoReleasePool,它起到了很好的作用——我的问题之一是我没有每次都为探查器构建我在做改变,却看不出有什么不同——忘记这件令人沮丧的蠢事