简体   繁体   English

Core Data永远不会释放从外部存储加载的NSData

[英]Core Data never release NSData loaded from external storage

So I use Core Data to store a few images. 所以我使用Core Data来存储一些图像。 (Also the subclass is generated with latest mogenerator ) (也使用最新的mogenerator生成子类)

(And also I'm using ARC) (而且我也在使用ARC)

Yeah I know I could just keep a reference and store it on the disk, but I thought : 是的,我知道我可以保留一个参考并将其存储在磁盘上,但我想:

"Hey they made an option so I can do just that without having to manage it myself!" “嘿,他们做了一个选择,所以我可以做到这一点,而不必亲自管理它!”

So I tried it and it works perfectly except all the data loaded that way is never released . 所以我尝试了它并且它完美地工作,除了所有加载的数据从未发布


在此输入图像描述

在此输入图像描述


In the initialization of the ViewController who's gonna be in charge of displaying the images I give it the usual main NSManagedObjectContext . 在ViewController的初始化中,谁将负责显示图像,我给它通常的主要NSManagedObjectContext

And in a method called in viewDidAppear I set up the UIScrollView with the images : viewDidAppear调用的方法中,我使用图像设置UIScrollView

Edit : So it's not really a fetch request I have an Entity1 which have one-to-many with images and I use it to get the images I get this entity1 from the same context. 编辑:所以它不是一个真正的获取请求我有一个Entity1 ,它有一对多的图像,我用它来获取我从同一个上下文得到这个entity1的图像。 I just wanted to simplify to explain better. 我只想简化以便更好地解释。

- (void)setupScrollViewWithEntity1:(Entity1 *)entity1 {
    DDLogVerbose(@"-- %@ : SETUP SCROLL VIEW --", self);
    // I remove any previous subviews
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.scrollView.contentSize = self.scrollView.frame.size;

    /* Here I get the imagesArray with a NSFetchRequest */ 
    //So it's not really a fetch request I have an `Entity1` which have one-to-many with images and I use it to get the images
    NSSet *imagesSet = entity1.images;


    // So I have an NSArray holding all the Image object
    for (Image *image in imagesSet) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = image.numberValue*frame.size.width;
        UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:frame];
        scrollView.contentSize = self.scrollView.frame.size;
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.scrollView.frame];
        imageView.image = [UIImage imageWithData:image.image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        [scrollView addSubview:imageView];
        scrollView.delegate = self;
        scrollView.minimumZoomScale = 1.0;
        scrollView.maximumZoomScale = 3.0;
        [self.scrollView addSubview:scrollView];
    }
}

} }

In viewWillDisappear I save the NSManagedObjectContext and I would expect when the controller gets dealloc'ed that all the data would too, but it stays in memory forever. viewWillDisappear我保存了NSManagedObjectContext ,我希望当控制器被释放时所有数据都会被释放,但它会永远保留在内存中。

This Line somehow retains it don't know why : imageView.image = [UIImage imageWithData:image.image]; 这条线以某种方式保留它不知道原因: imageView.image = [UIImage imageWithData:image.image];

I spent 3 days on it trying to use everything, Instruments , rechecked if I didn't keep a strong reference somewhere. 我花了3天时间尝试使用所有东西, Instruments ,如果我没有在某处保留强大的参考,则重新检查。

The thing is the UIViewController gets dealloc 'ed that I'm sure of, I see it in Instruments with the Allocation tool but for some reason the data stays in memory forever until the app crashes . 事情是UIViewController得到dealloc我确定,我在带有Allocation工具的Instruments看到它,但由于某种原因,数据永远保留在内存中直到应用程序崩溃


Here's the list of the ~20 images in memory not getting dealloc'ed : 这是内存中没有被解除分配的~20个图像的列表:

在此输入图像描述


And here's the details for the first object : 这是第一个对象的详细信息:

在此输入图像描述

Thank you for reading until here, I'm really desperate :( 感谢您阅读,直到这里,我真的很绝望:(

Have you tried calling reset on the NSManagedObjectContext after you are done with the images? 完成图像后,您是否尝试在NSManagedObjectContext上调用reset That evicts any loaded objects from the in-memory object graph. 这将从内存中的对象图中驱逐任何加载的对象。 They will then need to be fetched from disk/db next time they are required. 然后,需要在下次需要时从磁盘/ db获取它们。

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

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