Core Data

May 26, 2014

I have almost completely moved from a SQLite/FMDB setup to Core Data in my iOS apps. The main reason for this switch was due to how easy it is for me to get Core Data setup and have a nice object backed database. If you have read anything about Core Data in the past, great post by Brent Simmons talking about when he tried Core Data, you will know that depending on what app you are building Core Data can either be a godsend or a massive pain. For the types of apps I write Core Data works perfectly, and I still leave open the possibility to change from Core Data to SQLite/FMDB at any point if I find the switch worthwhile.

With that being said there are a few things that I miss a lot from the SQLite/FMDB setup and really wish Apple would fix. The biggest issue surfaces when you try to bulk update/delete records from the database. When you want to delete all records from a table with FMDB the code looks similar to this.

if ([appDatabase open]){
[appDatabase executeUpdate:@"DELETE FROM Checkins WHERE 1"];
[appDatabase executeUpdate:@"DELETE FROM Events WHERE 1"];
}

On Core Data though you first must load all the instances of the object in the database then iterate over each and delete them.

Core Data should have some kind of method that allows for the ability to remove or update all objects based on a NSPredicate, or something new, without the need to iterate over each item. I would even be fine with the ability to call deleteObjects: and pass in an array of objects over what you currently have to do. With WWDC 2014 coming up hopefully we will see some improvements to Core Data.

Side Note: You should really read Brent’s posts about his work with setting up sync for Vesper, it is one of the best developer diaries on the web.