Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data never release NSData loaded from external storage

So I use Core Data to store a few images. (Also the subclass is generated with latest mogenerator)

(And also I'm using 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.


enter image description here

enter image description here


In the initialization of the ViewController who's gonna be in charge of displaying the images I give it the usual main NSManagedObjectContext.

And in a method called in viewDidAppear I set up the UIScrollView with the images :

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. 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.

This Line somehow retains it don't know why : 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.

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.


Here's the list of the ~20 images in memory not getting dealloc'ed :

enter image description here


And here's the details for the first object :

enter image description here

Thank you for reading until here, I'm really desperate :(

like image 617
ItsASecret Avatar asked Nov 03 '22 03:11

ItsASecret


1 Answers

Have you tried calling reset on the NSManagedObjectContext after you are done with the images? 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.

like image 135
Steven Hepting Avatar answered Nov 13 '22 15:11

Steven Hepting