0
votes

I am using Encrypted CoreData Sqlite Store to encrypt my CoreData stack. I have a table in database which stores more than 400k records. I want to delete most of those records based on some activity. As batch delete is not available for this encrypted store what I need to do is, execute a fetch request to read data.

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "MyEntity")
fetchRequest.includesPropertyValues = false
// Fetch data till minimum threshold limit
let newPredicate = NSPredicate(format: "%K <= %@", "recordTime", NSNumber(value: maxAllowedTimeStamp as Int64))
fetchRequest.predicate = newPredicate
let objectsToDelete = myDataController.executeFetchRequest(fetchRequest) as? [NSManagedObject]

Than I perform delete operation one-by-one the objects:

for object in objectsToDelete {
    myDataController.deleteManagedObject(object)
}

This operation is taking more than 7-8 mins. I believe that core data is fast enough to provide fault object and it should not take this much time as you can see my predicate in which I am not taking any property value in order to reduce fetch time.

Can someone help me to understand what is taking so long to delete objects?

1
Hello Kapil batch delete option is best because iterating whole data and deleting will take lot of your time. try this link stackoverflow.com/questions/23201917/… for encryption/security of your data and try batch deletesDevanshu Saini

1 Answers

0
votes

What's taking so long is that you're deleting 400k objects one at a time. It's not about being encrypted, or at least not mainly about that. This kind of operation has always been slow, and is the reason that batch deletes were added.

Deleting this many records without batching is fundamentally a very slow process. If you can't use a batch delete, you need to think seriously about why you need to delete this many objects and whether you could avoid that. Maybe don't create so many if you're going to delete them all. Or maybe don't delete them-- since if you created them once, maybe you create them again later? Or if it's an offline cache of user account data, maybe don't fetch all of that data. Or maybe handle the delete by removing the persistent store files instead of by deleting the objects one by one.