0

I am deleting CoreData objects using this method:

    NSManagedObjectContext *theContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:nameEntity];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID
[fetchRequest setPredicate:predicate];
NSError *error;
NSArray *fetchedObjects = [theContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
    [theContext deleteObject:object];
}

error = nil;
if(![theContext save:&error]){
    NSLog(@"Couldn't save: %@", error);
}

What I don't understand is that for instance I download data and store it, and in the Settings I can see that my app uses 5MB of disk space. Once I delete the data using this method, it says my app uses 6.3MB of data. That makes absolutely no sense at all. What am I doing wrong? Why isn't the data being deleted correctly?

el-flor
  • 1,466
  • 3
  • 18
  • 39
  • Can you access the data after you've deleted it? Because if you can't, then the data *is* deleted correctly. I'm not sure that any documentation promises an immediate freeing of disk-space following a delete operation. (Somebody feel free to correct me if I'm wrong.) – Darren Black Oct 25 '15 at 10:20

2 Answers2

1

You can open the core data store with the SQLite vacuum option set to reclaim disk space. E.g.

NSDictionary *storeOptionsDict=@{NSSQLiteManualVacuumOption : @YES};
[persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType configuration: nil URL: sourceStoreURL options: storeOptionsDict error: &error];

See this older question: How to VACUUM a Core Data SQLite db?

Community
  • 1
  • 1
DDP
  • 2,463
  • 1
  • 26
  • 28
  • Thanks for the answer. I have done this, but I see no immediate effect. Is this normal? Thanks ! – el-flor Oct 25 '15 at 10:55
  • It worked for my OS X app (which used an older style delete journal-mode core data setup), reclaiming 30 MB from an 80 MB file. Perhaps things work differently with WAL mode and on iOS. Is the PSC setup code called when you expect? Maybe you can write a simplified test app to verify behaviour with your configuration. – DDP Oct 25 '15 at 11:13
  • Yes the PSC Setup code is called when I expect it to. I will make some more tests but thanks a lot for the info. – el-flor Oct 25 '15 at 11:17
0

When using the sqlite store, the storage is managed by sqlite, not the core data framework itself. When you delete a managed object, the row is deleted from the appropriate table. In a typical database, this does not result in freeing any disk space. As for why the space used might actually increase, this speculation on my part, but I think it's because of the transaction logging. Even though the transaction is complete, the log is not immediately purged.

I would recommend that you not worry about this. The sqlite library will have internal management to compact tables (reclaim space from deleted rows) and purge old transaction records.

Avi
  • 7,469
  • 2
  • 21
  • 22
  • Thanks a lot for the answer. The point is that I want to allow my users to delete some data if they need memory. So if the memory is needed, if will be deleted & freed? – el-flor Oct 25 '15 at 10:34