Search code examples
sqlitecore-datafmdb

Accessing CoreData tables from fmdb


I'm using CoreData in my application for DML statements and everything is fine with it. However I don't want use NSFetchedResultsController for simple queries like getting count of rows, etc.

I've decided to use fmdb, but don't know actual table names to write sql. Entity and table names don't match. I've even looked inside .sqllite file with TextEdit but no hope :) FMResultSet *rs = [db getSchema] doesn't return any rows

Maybe there's a better solution to my problem?

Thanks in advance


Solution

  • Core Data prefixes all its SQL names with Z_. Use the SQL command line tools to check out the your persistent store file to see what names it uses.

    However, this is a very complicated and fragile solution. The Core Data schema is undocumented and changes without warning because Core Data does not support direct SQL access. You are likely to make error access the store file directly and your solution may break at random when the API is next updated.

    The Core Data API provides the functionality you are seeking. IJust use a fetch request that fetches on a specific value using an NSExpressionDescription to perform a function. This allows you to get information like counts, minimums, maximums etc. You can create and use such fetches independent of a NSFetchedResultsController.

    The Core Data API is very feature rich. If you find yourself looking outside the API for a data solution, chances are you've missed something in the API.