When is CoreData object released after deletion + External Binary data?

Hello,

I am bulding app with ability to record video, audio and images. Every media is stored with CoreData entity and its binary data attribute with External storage (option for binary data in Core Data entity). For example one record can have many videos with relation below:

https://www.dropbox.com/s/jy5lftqde083gye/Screenshot%202017-08-08%2008.33.54.png?dl=0

https://www.dropbox.com/s/yus0jncxct9bw6l/Screenshot%202017-08-08%2008.34.23.png?dl=0


So far everything works well but during some performance testing I found that the app is not releasing CoreData object properly or at least predictably (for me).

First issue is that when I delete object from context and save it, it still remains in persistent memory because I can find it with my own predicated fetch. But for example default fetch for TableView (that code which is generated when you create new project) ignores the deleted object. I delete Record objects e.g. from table view this way:


override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
     if editingStyle == .delete {
          let context = fetchedResultsController.managedObjectContext
          let object:Record = fetchedResultsController.object(at: indexPath) as! Record 
          object.deleted_at = NSDate()
          context.delete(object)
          do {
               try context.save()
               print("saved")
          } catch {
               let nserror = error as NSError
               fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
          }
     } 
}



Next issue is that I am not sure if cascading deletion is performed in this moment...

Which points me to third and last issue - the binary data files remain in app memory (checked with downloading App Container via Xcode). This major issue causes the app uncontrollably "expands".

I was believing that External storage solves this issue automatically. Even when I manually nil the variable which holds the binary data, real file still remains in persistent memory.

So questions may sound like this:

1. When Core Data releases object really permanently from persistent memory?

2. When is External binary data deleted from persistent memory?

Thanks all of you for your help!

J.

1. Core Data entities aren't removed from the persistent store until the changes are committed to the persistent store. If you are using nested contexts (which some of the standard code does), then there's an arbitrary delay between saving the context, the changes reaching the parent context, and the parent context being saved (and thus committing the changes to the persistent store).


Note that setting an attribute on a deleted managed object is generally a terrible thing to do, because the managed object's attributes may become faults at different times depending on whether the managed object was still considered temporary.


2. As far as I know, no promises are made concerning the timeliness of deleting the external data file from the device's flash drive.


But I'm concerned that you're using terminolgy to mean something else when you talk about nil'ing the variable holding the binary data.

When is CoreData object released after deletion + External Binary data?
 
 
Q