I have an entity named Image with a binary attribute named imageData. I need to cycle trough all Image objects and do something with the data contained inside the imageData attribute.
This is the code:
for image in managedObjectContext.allImages {
autoreleasepool {
var imageData = image.imageData
}
}
I have a lot of objects and every imageData has something like 500KB of data for a total of 3GB of data.
The problem is that each time the imageData attribute is used, the data go into memory and not released until the loop is done. This is causing the memory to grow up to 3GB crashing the app.
I also tried to turn the object into a fault when I'm done with it by calling refresh(_:mergeChanges:) but nothing changes:
for image in managedObjectContext.allImages {
autoreleasepool {
var imageData = image.imageData
managedObjectContext.refresh(image, mergeChanges: false)
}
}
How can I cycle trough all objects without filling up all the memory?
Thank you