views:

391

answers:

1

Hi, all!

I’m in the process of designing an iPhone app, using Core Data for data persistence (with local SQLite store[s]). Before I get too far into the implementation, I’m hoping for some qualitative advice on some basic design questions relating to Core Data.

Here’s the scenario:

  • The app handles data on dozens (possibly hundreds) of Students -- EACH with several Hundred individual items of data (numbers and text).

  • Most of the time, the user of the app will be accessing/modifying information on one student (at a time).

  • Occasionally, they’ll pop back to a directory view, which shows a list of all students to choose from.

So here’s the first big design question:

CORE DATA STORES

In such an application, what’s the most reasonable approach to storing all this data: with a single data store, or perhaps multiple stores?

Approach #1: Would be using ONE data store for all the information on ALL the students.

Approach #2: Use of MULTIPLE stores, one for each student. (Remember that there’s hundreds of items of data on each and every one.)

Approach #3: Perhaps a hybrid approach: Multiple stores, one for each student, with a separate ‘index’ file used to associate basic metadata on all students with the filename of each student’s individual store.

~~~

-- Assuming for a moment, approach #2: This means simplicity when accessing a particular student’s data. However, in order to generate the ‘directory’ screen which lists all students, I’d need to iterate through each of the stores to tabulate the information needed to assemble that list (first name, last name, and a few other items for each person -- so using just each store's filename to represent that metadata isn’t sufficient, or desirable).

In this case, I’d need to OPEN each of the stores, retrieve the student’s basic metadata, then close it up again & move to the next file.

Is this inefficient? Is it expensive to open/close potentially dozens (or hundreds) of individual stores?

[In fact, I’d already started in with approach #2, but have gotten hung up in iterating across multiple stores to create that ‘directory’ view -- hence, I’m wondering if it’s a reasonable approach to begin with.

The potential considerations included: keeping the size of each file manageable; the design of the object graph a little simpler; and also to minimize the potential for data loss if a store should somehow become corrupt -- but I’m not sure how much of a practical issue that really is.]

CORE DATA STACKS

-- Next, and again assuming approach #2: How to actually do that?

To open/read/close a particular store, should I reuse a singleton Context? ... Coordinator? (i.e., first, Remove any opened store from that Context, Add the next store, and iterate through that way?) Or release & reconstruct the stack for each store I need to iterate through?

(One possible consideration would be to eliminate the potential of commingling data between files.)

And which of these operations are expensive?

... Or would it much simpler just to use one large-ish store to handle all data in the first place? :O

~~~

Anyway, I realize this is an elaborate question, but any guidance would be VERY much appreciated.

I also hope this helps others using Core Data for their apps as well.

THANK YOU so much in advance!

+4  A: 

Hundreds of students with hundreds of items will generally not be an issue for an sqlite3 CoreData store, so using a single store is just fine. You will probably want to think about what fields you want indexed, but other than that its all fine.

In any event, opening and closing each store will be tremendously slower than pulling the data from a single store at these scales. If you really find you need to shard the data it will be a lot more complex to do it in a reasonable way, and I would not look at dealing with it until you actually have performance numbers that say you do. Don't worry about wasting work doing it the simple way, since that will be almost no work compared a sharded version and will still allow you get the basic application running in order to start evaluating your UI and testing the apps usability.

Louis Gerbarg
Thank you, Louis! Great points. That makes a lot of sense.And just to be clear: this would mean potentially tens of thousands of objects, all in the same data store?
rondoagogo
Core Data operates easily on >1e6 objects using the SQLite store as long as you don't have too many instances in memory in the managed object context at one time (a standard consideration on the iPhone).
Barry Wark
Yeah, what Barry said. I didn't want to get too specific since the exact points at which perf drops off can change if you have tons of indexes or BLOBs, but in general usage hundreds of thousands to millions is fine on the phone.
Louis Gerbarg
rondoagogo