I'm not sure whether this is a bug or not, so am asking for advice...
(iOS 7.1.2 on xcode 5.1.1)
My app stores many large data image in coredata. The binary images have their attribute set in the entity to 'Allows External Storage', so I see a file (guid) appear in the _EXTERNAL_DATA sub-folder for my app.
During the lifetime of this app, the file will change regularly and so I overwrite the existing image and save the context.
The problem is, I'm seeing orphaned copies of my image files (guids) appearing, as new ones are created, but the old ones are not deleted.
This can be reproduced as follows...
Create a utility app with a 'test' button on it that utilises coredata, creating a simple entity...


Create the initial entity in the viewDidLoad, storing a reference to it....
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
id delegate = [[UIApplication sharedApplication]delegate];
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
// Create initial external file
_testEntity = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:managedObjectContext];
UIImage *planeImage = [UIImage imageNamed:@"plane.jpg"];
_testEntity.image = [NSData dataWithData:UIImagePNGRepresentation(planeImage)];
[delegate saveContext];
}
Then in an action handler for a button on the view, simply change the image...
-(IBAction)onTestImageButton:(id)sender { int randNum = rand() % 4 + 1;
id delegate = [[UIApplication sharedApplication]delegate]; // Store image - again UIImage *planeImage = [UIImage imageNamed:[NSString stringWithFormat:@"plane %d.jpg", randNum]]; _testEntity.image = [NSData dataWithData:UIImagePNGRepresentation(planeImage)]; [delegate saveContext]; }
Here, I have four large jpg's of a plane, each one slightly different size. (If they are the same size, the problem doesn't manifest itself)
Run the app and press the 'test' button several times. Soon, several versions of the file appear in _EXTERNAL_DATA

I would only ever expect there to be one version. Images are now orphaned and if a parent entity deletes this one via cascade delete rules, files are left behind, which take valuable space!
Is this a bug, or am I doing something wrong?
Thanks