lundi 24 août 2015

deleting binary data from Core Data iOS 8

I have an app that allows users to download images from a website. (Toy Collector Checklist 2 - http://ift.tt/1HYZqqh)

The user could theoretically download thousands of images, creating quite a large usage hog. I'm trying to add a "Delete All Images" so a user can flush out the images without deleting the app.

The code seems to work. The images are no longer visible when the button is pressed, and the core data value returns "nil" when I check it - but when I go to my usage settings the space doesn't appear to be freed up. I'm using what I would consider pretty standard fetch and update code:

CoreDataHelper *cdh =
[(AppDelegate *)[[UIApplication sharedApplication] delegate] cdh];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Item"
                               inManagedObjectContext:cdh.context];
[fetchRequest setEntity:entity];
//   [fetchRequest setPredicate:nil];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"photo != nil OR photoAlt != nil OR photoCam != nil"]];
[fetchRequest setFetchBatchSize:500];
NSManagedObject *removePhotoFromObject;
NSError *error = nil;
NSArray *results = [cdh.context executeFetchRequest:fetchRequest error:&error];
for (removePhotoFromObject in results) {
    [removePhotoFromObject setValue:nil forKey:@"photoCam"];
    [removePhotoFromObject setValue:nil forKey:@"photo"];
    [removePhotoFromObject setValue:nil forKey:@"photoAlt"];
    [removePhotoFromObject setValue:nil forKey:@"thumbnail"];



}
[cdh backgroundSaveContext];

That sets the value of the possible photo locations to nil. If I run the same code again, it doesn't add them into the array (because their values are now nil), so I know that part is working. And the images and thumbnails are removed. But the data usage doesn't change when I check in the Settings tab.

Something that may be mucking up the workings:

Item is an entity. photo, photoAlt, and photoCam are ALSO each entities, with a "To One" relationship with Item

They are all attributed as "Binary Data" in the model.

Any thoughts and suggestions would be greatly appreciated. I have looked on these forums, as well as the larger internets, but have so far been unable to find a solution.

Zack

Aucun commentaire:

Enregistrer un commentaire