首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS下载&解析大型JSON响应导致CFData (存储)泄漏

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

Stack Overflow用户
提问于 2013-01-31 20:40:05
回答 1查看 2.3K关注 0票数 0

当用户第一次打开我的应用程序时,我需要下载大量的数据。我以JSON的形式从服务器获取所有这些数据。根据用户的不同,这些JSON文件可以位于10 of 30 of之间的任何位置,并且有它们的10+。

当JSON的记录不超过500条左右时,我这样做没有问题,但正如我所说的,有些记录有10,000+记录,大小可以高达30 to。

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

,看来CFData必须是我在didReceiveData中建造的NSMutableData。当我下载一个JSON时,CFData (存储)就会上升--当我开始解析它时,它就会停止上升。

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

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

--

深入研究CFData并不能给我带来多大帮助:

下面是我创建操作以获取这些JSON的代码--

代码语言:javascript
复制
- (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流..。这能帮我减少内存中的对象数量吗?

此外,我唯一能想到的其他选择是实现某种paging..but,我需要我的用户拥有所有的数据。

我们非常感谢您的任何帮助和建议!谢谢!

--

编辑

好的,我决定使用去掉AFNetworking并尝试使用本机函数。在这样做的过程中,我仍然得到了同样的CFData构建。我正在使用NSOperation的这个子类,现在我正在创建/添加操作,如下所示:

代码语言:javascript
复制
 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];

 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-31 21:49:08

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

我只需使用标准的NSURLConnection下载NSData,然后执行以下操作.

代码语言:javascript
复制
NSData *data = [NSURLConnection sendSynchronous...

// use NSDictionary or NSArray here
NSArray *array = [NSJSONSerialization JSONObjectWithData:data ...

Now deal with the objects.

没有泄漏,因为它是一个本机函数。比第三部分框架要快得多。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14634736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档