iOS下载和parsing大的JSON响应导致CFData(存储)泄漏

用户第一次打开我的应用程序,我需要下载大量的数据。 我以JSONforms从服务器获取所有这些数据。 根据用户的不同,这些JSON文件可以在10kb到30mb的范围内,其中有10个以上。

当JSON不超过500条logging时,我没有任何问题,但是正如我所说的,有些logging超过了10,000条,最多可以达到30MB。

当下载较大的JSON时,我的应用程序会分配大量的内存,直到我最终得到内存警告,应用程序崩溃。

看来CFData必须是我在didReceiveData中构build的NSMutableData。 当我下载一个JSON时,CFData(store)就会上升 – 当我开始parsing时,它停止上升。

在继续下载和parsing下一个JSON之前,如何清除这些数据?

如下所示,内存中有200mb的CFData(存储):

在这里输入图像说明

挖入CFData并不能帮助我: 在这里输入图像说明

这里是我创build操作来获取这些各种JSON的代码 –

- (void)checkForUpdates { if(!_globals) _globals = [MySingleton sharedInstance]; MyAFHTTPClient* client = [MyAFHTTPClient sharedClient]; NSString* path = [NSString stringWithFormat:@"cache?deviceUID=%@&token=%@",[_globals getMacAddress], [_globals getToken]]; NSURLRequest* request = [client requestWithMethod:@"GET" path:path parameters:nil]; _currentEnvironment = [_globals getActiveEnvironment]; if(!_currentEnvironment.didDownloadDataValue) [self setupAndShowHUDinView:self.view withText:@"Checking for updates..."]; AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { for(NSString *str in [JSON valueForKey:@"Views"]) { //for each table i need to update, create a request to NSString* path = [NSString stringWithFormat:@"cache/%@/?deviceUID=%@&token=%@", str, @"00000-00000-0000-00001", [_globals getToken]]; NSURLRequest* request = [client requestWithMethod:@"GET" path:path parameters:nil]; AFJSONRequestOperation* operation2 = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { // even if this is comment this out and I do nothing but download, app crashes //[self updateTable:str withJSON:JSON]; } failure:nil]; [operation2 setSuccessCallbackQueue:backgroundQueue]; [client.operationQueue addOperation:operation2]; numRequests++; } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { }]; [operation start]; } 

那CFData是我在内存中的JSONs响应吗? 我试图清除我的NSURLCache没有运气 –

我也看了一下JSONstream..会帮我减less内存中的对象的数量吗?

除此之外,我能想到的唯一的其他select是实现某种分页……但我需要我的用户拥有所有的数据

任何帮助/build议非常感谢! 谢谢!

编辑

好吧,我决定使用去除AFNetworking并尝试使用本机function。 在这样做,我仍然得到相同的CFData的构build。 我正在使用NSOperation的这个子类,现在我正在创build/添加我的操作:

  NSOperationQueue *operationQueue; operationQueue = [[NSOperationQueue alloc]init]; [operationQueue setMaxConcurrentOperationCount:1]; for(NSString *str in [views valueForKey:@"Views"]) { NSString* path = [NSString stringWithFormat:@"%@cache/%@/?deviceUID=%@&token=%@", _globals.baseURL, str, @"00000-00000-0000-00001", [_globals getToken]]; LibSyncOperation *libSyncOperation = [[LibSyncOperation alloc] initWithURL:path]; [operationQueue addOperation:libSyncOperation]; } 

我使用本机JSON转换函数与大量的数据没有内存问题。

我只是使用标准的NSURLConnection下载NSData然后执行以下操作…

 NSData *data = [NSURLConnection sendSynchronous... // use NSDictionary or NSArray here NSArray *array = [NSJSONSerialization JSONObjectWithData:data ... Now deal with the objects. 

没有泄漏,因为它是一个本地function。 比第三部分框架快得多。