저는 몇 주 동안이 문제를 해결하기 위해 노력해 왔습니다. 해결할 수없는 것 같습니다. 나는 데이터베이스에서 액션을 수행하기 위해 모든 엔티티를 반복하고있다. 나는 NSFetchRequest를 잠시 사용하고 있었지만, 멈추려는 시도에도 불구하고 각 반복마다 메모리 사용량이 계속 증가하여 각 반복 후에 메모리를 다시 얻지 못했습니다. 이제 NSFetchedResultsController 같은 작업을 사용하고 있습니다. 그것을 통해 실행코어 데이터를 처리 할 때 메모리 구축을 어떻게 중지합니까?
- (NSFetchedResultsController *)updateController {
if (_updateController != nil) {
return _updateController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Entry" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"creationDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil
cacheName:nil];
self.updateController = theFetchedResultsController;
return _updateController;
}
그리고 코드 :
NSFetchedResultsController :
:NSLog(@"update controller: %@", [self updateController]);
if (![[self updateController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
int i = 0;
id sectionInfo = [[_updateController sections] objectAtIndex:0];
NSLog(@"count: %i", [sectionInfo numberOfObjects]);
NSManagedObject *entry;
while (i < [sectionInfo numberOfObjects]) {
@autoreleasepool {
entry = [_updateController.fetchedObjects objectAtIndex:i];
NSLog(@"entry: %@", [entry valueForKey:@"message"]);
NSLog(@"context 1: %@", [entry managedObjectContext]);
NSLog(@"context 2: %@", [[self updateController] managedObjectContext]);
[[entry managedObjectContext] refreshObject:entry mergeChanges:NO];
NSLog(@"i: %i", i);
i++;
}
}
인스트루먼트는이 보여줍니다
이
은 내가 가지고있는 코드입니다타임 라인에서 해당 기간을 클릭하면 테이블에서 해당 항목을 선택합니다. 데이터를 사용하는 작업이 진행되는 동안 그래프의 기간이 1.14MB에서 1.17MB로 약간 증가합니다. 이 단계에서는 그리 많지 않지만 NSData 이미지와 같이 더 큰 데이터를 처리하는 코드를 추가하면 앱 메모리 사용량이 결국 메모리가 부족해져 충돌이 발생할 때까지 증가합니다. 앱의 1.0 버전은 이미 앱 스토어에 있습니다. 따라서 핵심 데이터에 NSData가없는 것이 옵션이 아닙니다.
누군가가 도와 드릴 수 있기를 바랍니다.
핵심 데이터 개체를 많이 업데이트하는 경우 주기적으로 저장해야 변경 사항을 디스크에 쓸 수 있습니다. 그때까지는 메모리에 보관해야합니다. 샘플 코드에서 엔티티에 대해 변경 한 사항을 실제로 볼 수 없습니다. – jrturton
@jrturton 샘플 코드에서 변경하지 않을 것입니다. 그래도 여전히 메모리가 증가하고 있습니다. – Andrew