访问NSManagedObject的data属性给我内存问题 [英] Accessing data attribute of NSManagedObject gives me memory issues
问题描述
我认为我可能访问了这个错误,也许有人可以指出正确的方向。
I think I might be accessing this wrong, perhaps somebody could point me in the right direction.
我有一个条目
实体,它与 Media
实体具有一对多关系。 Media实体包含一个名为 originalImage
的数据属性。
I have a Entry
entity, which has a one-to-many relationship with the Media
entity. The Media entity contains a data attribute called originalImage
.
前50个条目
The first 50 Entry
entities that this iterates through do not have any media
items in the set. After that, it slows down as it accesses the originalImage
attribute and eventually runs out of memory and quits to the home screen, without any message other than a previous memory warning in NSLog. Running this through Instruments also shows it running out of memory at this point. Here's my code:
NSFetchRequest *oldFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *oldEntryEntity = [NSEntityDescription entityForName:@"Entry"
inManagedObjectContext:oldMOC];
[oldFetchRequest setEntity:oldEntryEntity];
[oldFetchRequest setFetchBatchSize:10];
NSArray *entrys = [oldMOC executeFetchRequest:oldFetchRequest error:nil];
for (NSInteger index = 0; index < entrys.count; ++index) {
Entry *entry = [entrys objectAtIndex:index];
NSOrderedSet *oldMediaSet = [entry valueForKey:@"media"];
for (Media *media in oldMediaSet) {
@autorelease {
[media valueForKey:@"originalImage"];
}
}
}
删除原始图像行意味着它不会崩溃,这就是让我相信它与被访问有关的原因。
Removing that originalImage line means it doesn't crash, which is what makes me believe it's related to that being accessed. Perhaps the way I get the orderedSet means these items stay in memory?
推荐答案
核心数据正在满足您所使用的属性的缺点访问,直到它填满了可用内存。您正在尝试使用 @autorelease
进行防御,但这还不够,因为您没有获得自动发布的实例,而是获得对象图中的另一个对象。不知道您要做什么,很难说如何解决问题。您正在一个循环中进行同步迭代,直到内存用完,因此不会出现任何内存警告来中断此过程。
Core Data is fulfilling the faults for the properties you are accessing, until it has filled up its available memory. You are trying to defend against this with @autorelease
but that's not sufficient because you are not getting an autoreleased instance but rather another object in the object graph. Without knowing what you're trying to do it's hard to say how to resolve the issue. You're synchronously iterating in a loop until running out of memory so no memory warning is going to interrupt this process.
这篇关于访问NSManagedObject的data属性给我内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!