将大量数据保存到核心数据会导致应用因内存问题而终止

时间:2017-06-15 02:22:07

标签: objective-c core-data memory-management nsarray

我正在将大量项目下载到NSArray中,因为我需要在本地将所有项目都用于离线目的,一旦将数据下载到NSURLSession竞争处理程序中的NSArray,我的应用程序的内存使用量相当适中大约120 MB,但是当我开始循环这个NSArray以将数据插入核心数据并在本地保存时,内存峰值非常快,然后由于内存问题导致应用程序终止。我已经尝试将NSArray拆分为数组的NSArray,以便在每个批处理保存到磁盘后尝试释放内存,但这也无济于事,所以我必须遗漏一些东西。我究竟做错了什么?我需要保持较低的内存占用率,以便我可以下载数据,并且它不是延迟加载的选项,只能下载所需的内容等,因为我必须提供离线方案,所以我绝对必须在本地拥有数据。

简而言之

完成处理程序启动循环时的内存使用量约为120 MB 然后它迅速增加到2 GB并且app终止 应用程序始终处于前台

我附上下载并保存数据的代码

+ (void)fetchTillDataAll:(int)tillId :(int)startAtRow :(int)takeNoOfRows {  

    if ([NWTillHelper isDebug] == 1) {  
        NSLog(@"WebServices:fetchTillDataAll:tillId = %d, startAtRow = %d, takeNoOfRows = %d", tillId, startAtRow, takeNoOfRows);  
    }  

    NSString *finalURL = [NSString stringWithFormat:@"https://host.domain.com/api/foo/bar];  

    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:finalURL]  
                                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {  

                                     if (error != nil) {  
                                         if ([NWTillHelper isDebug] == 1) {  
                                             NSLog(@"WebServices:fetchTillDataAll:Transport error %@", error);  
                                         }  
                                     } else {  
                                         NSHTTPURLResponse *responseHTTP;  
                                         responseHTTP = (NSHTTPURLResponse *) response;  

                                         if(responseHTTP.statusCode != 200) {  
                                             if ([NWTillHelper isDebug] == 1) {  
                                                 NSLog(@"WebServices:fetchTillDataAll:Server Error %d", (int) responseHTTP.statusCode);  
                                             }  
                                         } else {  
                                             NSArray *tillBasicDataArray = [NSJSONSerialization JSONObjectWithData:data  
                                                                                                           options:0  
                                                                                                             error:NULL];  
                                             if ([NWTillHelper isDebug] == 1) {  
                                                 NSLog(@"WebServices:fetchTillDataAll:tillBasicDataArray count = %lu", (unsigned long)[tillBasicDataArray count]);  
                                                 NSLog(@"WebServices:fetchTillDataAll:tillBasicDataArray looks like %@",tillBasicDataArray);  
                                             }  

                                             AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];  


                                             NSPersistentContainer *container = appDelegate.persistentContainer;  

                                             NSArray *arrayOfArrays = [NWTillHelper splitIntoArraysOfBatchSize:tillBasicDataArray :1000];  

                                             for(NSArray *batch in arrayOfArrays) {  

                                                 [container performBackgroundTask:^(NSManagedObjectContext *context ) {  
                                                     context.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy;  

                                                     NSDictionary *tillBasicDataDict = Nil;  

                                                     /  
                                                     /  
                                                     for (id element in batch){  
                                                         tillBasicDataDict = element;  

                                                         NSString *itemId = [tillBasicDataDict objectForKey:@"itemId"];  
                                                         NSString *brandId = [tillBasicDataDict objectForKey:@"companyId"];  
                                                         NSString *languageId = [tillBasicDataDict objectForKey:@"languageCode"];  
                                                         NSString *colorCode = [NSString stringWithFormat:@"%@", [tillBasicDataDict objectForKey:@"colorCode"]];  
                                                         NSString *discountable = [tillBasicDataDict objectForKey:@"discountable"];  
                                                         NSString *exchangeable = [tillBasicDataDict objectForKey:@"exchangeable"];  
                                                         NSString *noos14 = [tillBasicDataDict objectForKey:@"noos14"];  
                                                         NSString *sizeCode = [NSString stringWithFormat:@"%@", [tillBasicDataDict objectForKey:@"sizeCode"]];  
                                                         NSString *taxGroup = [tillBasicDataDict objectForKey:@"taxGroupId"];  
                                                         NSString *taxRegion = [tillBasicDataDict objectForKey:@"taxRegion"];  
                                                         NSString *tradeItemDesc = [tillBasicDataDict objectForKey:@"tradeItemDesc"];  
                                                         NSString *withTax = [tillBasicDataDict objectForKey:@"withTax"];  
                                                         NSString *status = [tillBasicDataDict objectForKey:@"status"];  

                                                         /  


                                                         NSManagedObject *newPimItem = Nil;  
                                                         newPimItem = [NSEntityDescription  
                                                                       insertNewObjectForEntityForName:@"TillData"  
                                                                       inManagedObjectContext:context];  

                                                         [newPimItem setValue:itemId forKey:@"itemId"];  
                                                         [newPimItem setValue:brandId forKey:@"brandId"];  
                                                         [newPimItem setValue:languageId forKey:@"languageCode"];  
                                                         [newPimItem setValue:colorCode forKey:@"colorCode"];  
                                                         [newPimItem setValue:discountable forKey:@"discountable"];  
                                                         [newPimItem setValue:exchangeable forKey:@"exchangeable"];  
                                                         [newPimItem setValue:noos14 forKey:@"noos14"];  
                                                         [newPimItem setValue:sizeCode forKey:@"sizeCode"];  
                                                         [newPimItem setValue:[NSNumber numberWithInt:[taxGroup intValue]] forKey:@"taxGroup"];  
                                                         [newPimItem setValue:taxRegion forKey:@"taxRegion"];  
                                                         [newPimItem setValue:tradeItemDesc forKey:@"tradeItemDesc"];  
                                                         [newPimItem setValue:[NSNumber numberWithInt:[withTax intValue]] forKey:@"withTax"];  
                                                         [newPimItem setValue:[NSNumber numberWithInt:[status intValue]] forKey:@"status"];  

                                                         if ([NWTillHelper isDebug] == 1) {  
                                                             NSLog(@"WebServices:fetchTillDataAll:ItemId in loop = %@", itemId);  
                                                             NSLog(@"WebServices:fetchTillDataAll:newPimItem = %@", newPimItem);  
                                                             NSLog(@"WebServices:fetchTillDataAll:CoreData error = %@", error);  
                                                         }  

                                                     }  
                                                     NSError *error = nil;  
                                                     if (![context save:&error]) {  
                                                         NSLog(@"Failure to save context: %@\n%@", [error localizedDescription], [error userInfo]);  
                                                         abort();  
                                                     } else {  
                                                         NSUserDefaults *tillUserDefaults = [NSUserDefaults standardUserDefaults];  
                                                         [tillUserDefaults setInteger:1 forKey:@"hasTillData"];  
                                                         [tillUserDefaults synchronize];  
                                                     }  
                                                 }];  
                                             }  
                                         }  
                                     }  
                                 }] resume];  
}  

NSArray拆分方法如下所示

+ (NSArray *) splitIntoArraysOfBatchSize:(NSArray *)originalArray :(int)batchSize {  

    NSMutableArray *arrayOfArrays = [NSMutableArray array];  

    for(int j = 0; j < [originalArray count]; j += batchSize) {  

        NSArray *subarray = [originalArray subarrayWithRange:NSMakeRange(j, MIN(batchSize, [originalArray count] - j))];  
        [arrayOfArrays addObject:subarray];  
    }  

    return arrayOfArrays;  
} 

----编辑----

我甚至尝试将下载分成1000个记录的批次,但这也无济于事。

while ( loopCount < numberOfLoops ) {
            if([NWTillHelper isDebug] == 1) {
                NSLog(@"%s loopCount = %d", __PRETTY_FUNCTION__, loopCount);
                NSLog(@"%s startAtRow = %d", __PRETTY_FUNCTION__, startAtRow);
                NSLog(@"%s records to fetch = %@", __PRETTY_FUNCTION__, recordsToFetchStr);
            }

        //[self fetchPricelistAll:(int)[NWTillHelper getPricelist] :0 :recordsToFetch];
        [self fetchPricelistAll:(int)[NWTillHelper getPricelist] :startAtRow :batchSizeInt];

        startAtRow = startAtRow + batchSizeInt;

        loopCount++;
    }

2 个答案:

答案 0 :(得分:0)

我建议拆分Web服务响应以提供位数据。您可以从服务器下载位并将其作为文本文件保存在文档文件夹中。从文本文件中依次获取数据并将其保存在核心数据中。保存到核心数据完成后删除文件。

答案 1 :(得分:0)

我们可以按照以下方法

  1. 创建100/1000记录后,在上下文中调用save方法。
  2. 保存后我们可以调用reset方法一个上下文

    [managedObjectContext reset];