简体   繁体   English

核心数据触发错误将对象添加到NSMutableArray?

[英]Core Data Firing fault adding objects to NSMutableArray?

I have a problem with Core Data and NSMutableArray. 我对核心数据和NSMutableArray有问题。 Reading this document: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html#//apple_ref/doc/uid/TP40003468-SW2 in the chapter "Faulting Behavior" I read: Since isEqual and hash do not cause a fault to fire, managed objects can typically be placed in collections without firing a fault. 阅读本文档: https : //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdPerformance.html#//apple_ref/doc/uid/TP40003468-SW2我读到:由于isEqual和hash不会引发错误,因此通常可以将托管对象放入集合中而不会引发错误。

Ok, for this reason, I understand that I can: - fetch the managedObjectContext - put all managed object into an array (the objects contains image data) 好的,基于这个原因,我知道我可以:-获取managedObjectContext-将所有托管对象放入一个数组(这些对象包含图像数据)

without firing a fault and waste memory (until the object is accessed for the first time), correct? 不会引发故障并浪费内存(直到首次访问该对象),对吗?

But, for some reason, Core Data is firing a fault when I try to put the result in an NSMutableArray 但是,由于某种原因,当我尝试将结果放入NSMutableArray中时,Core Data会触发错误

NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:request error:&error]; //this line does'n fire a fault
self.cache = [NSMutableArray arrayWithArray:fetchResults]; //this line fires a fault

self.cache is simply a NSMutableArray. self.cache只是一个NSMutableArray。 After the last line of code, I see the memory usage growing through instruments (I have 50MB of images in the DB, and the memory goes immediately from 2-3Mb to 52-53MB. 在最后一行代码之后,我看到通过仪器的内存使用量正在增长(数据库中有50MB的图像,内存立即从2-3Mb变为52-53MB。

Any suggestion? 有什么建议吗? Thanks 谢谢

Ok, It was my mistake looking only to the Instruments memory occupation to determine if the fault was firing. 好的,这是我的错误,仅查看Instruments的内存占用来确定故障是否正在触发。

Core Data documentation says: If you need to determine whether an object is a fault, you can send it an isFault message without firing the fault. Core Data文档说:如果需要确定对象是否为故障,则可以发送isFault消息而不触发故障。 If isFault returns NO, then the data must be in memory. 如果isFault返回NO,则数据必须在内存中。 However, if isFault returns YES, it does not imply that the data is not in memory. 但是,如果isFault返回YES,则并不意味着该数据不在内存中。 The data may be in memory, or it may not, depending on many factors influencing caching. 数据可能在内存中,也可能不在内存中,这取决于许多影响缓存的因素。

I added this code after the "incriminate" lines: 我在“ incriminate”行之后添加了以下代码:

for (ImageCache *cache in self.cache) {
    NSLog(@"Is fault? %i", [cache isFault]);
}

The result was 1 for all the objects. 所有对象的结果均为1。

Then I modified the for loop: 然后我修改了for循环:

for (ImageCache *cache in self.cache) {
    NSLog(@"Is fault? %i", [cache isFault]);
    UIImageView *imageView = [[UIImageView alloc]initWithImage:cache.image];
    NSLog(@"Is fault? %i", [cache isFault]);
}

The result was 1 for the first NSLog, and 0 for the second NSLog of each object (the fault fired after the access to the image) 每个对象的第一个NSLog的结果为1,第二个NSLog的结果为0(在访问映像后触发的错误)

As the documentations says, it seems that Core Data is correctly faulting my objects, the memory occupation is due to Core Data caches. 正如文档所述,Core Data似乎正在正确地对我的对象进行故障处理,内存占用是由于Core Data缓存引起的。 Mea culpa :-) (although I'm still curious to see how it behaves in real low memory situations. I expect this cache to flush...trying to send a memory warning has no effects on the memory size) Thanks Mea culpa :-)(尽管我仍然很想知道它在真正的内存不足情况下的行为。我希望此缓存能够刷新...尝试发送内存警告对内存大小没有影响)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM