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.
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 :
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? 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With