iOS 下载和解析大型 JSON 响应导致 CFData(存储)泄漏

标签 ios memory-management out-of-memory nsoperationqueue cfdata

用户第一次打开我的应用程序时,我需要下载大量数据。我以 JSON 格式从服务器获取所有这些数据。根据用户的不同,这些 JSON 文件的大小可能在 10kb 到 30mb 之间,并且有 10 多个。

当 JSON 的记录不超过 500 条左右时,我可以毫无问题地执行此操作,但就像我说的那样,有些记录有 10,000 多条记录并且大小可达 30mb。

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

CFData 似乎必须是我在 didReceiveData 中构建的 NSMutableData。当我下载单个 JSON 时,CFData(存储)上升——当我开始解析时它停止上升。

如何在继续下载和解析下一个 JSON 之前清除该数据?

正如您在下面看到的,内存中有 200mb 的 CFData(存储):

enter image description here

--

深入研究 CFData 对我没有太大帮助: enter image description here

这是我创建操作以获取这些不同 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 是我的 JSON 响应吗?我试图清除我的 NSURLCache 但没有成功——

我还研究了一些 JSON 流。这是否有助于我减少内存中的对象数量?

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

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

--

编辑

好吧,我决定使用剥离 AFNetworking 并尝试使用 native 功能。在这样做的过程中,我仍然得到相同的 CFData 构建。我正在使用 this subclass of NSOperation我现在正在创建/添加我的操作如下:

 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.

没有泄漏,因为它是 native 函数。比第三方框架快很多。

关于iOS 下载和解析大型 JSON 响应导致 CFData(存储)泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14634736/

相关文章:

ios - 超过最大触摸次数使得程序不调用touchesEnded方法?

ios - 预期标识符

java - Android 内存不足错误

java - 堆不想更改大小(java.lang.OutOfMemoryError)android studio

ios - 正确管理调度队列以减少iOS手机发热

iphone - [self.view addSubview :_topViewController. View ];自转问题

ios - 此行的正确内存管理? (Cocos2D)

带有字符串键的Android SparseArray?

swift - Assets 单例和引用循环

java - OutOfMemoryError - 为什么等待线程不能被垃圾回收?