2

I have read up on Core Data and SQLite 3 however I am not sure which would be best for me.

I am getting a list of appointments from our API and will then need to store them. I will need to reference them based on date range, employee, customer etc. From what I have read SQLite3 would be the best for retrieving appointments that occur during a time range and appointments assigned to certain customers and employees.

I read that Core Data is the way to go however it doesn't seem like it can function how I would like. Can someone explain this a bit more based on my needs and let me know which would work best? If I were to use SQLite3 would FMDB be the best?

Update 1:

One thing that concerns me with going with core data for this is that Appple seems to not like applications that use Core Data with multi threads.

Bot
  • 11,868
  • 11
  • 75
  • 131
  • Lol, sorry must have mixed links up. The answer you seek lies here: http://stackoverflow.com/questions/523482/core-data-vs-sqlite-3 – JoeCortopassi Feb 21 '12 at 23:33
  • @JoeCortopassi that is not the answer I seek. I have already read that but am needing specifics based on my needs. – Bot Feb 21 '12 at 23:34
  • 1
    Or you can read my answer here: http://stackoverflow.com/questions/8723923/core-data-vs-sqlite-or-fmdb/8736032#8736032 . – adonoho Feb 22 '12 at 13:22
  • 1
    See also [Use CoreData or SQLite on iPhone?](http://stackoverflow.com/questions/1318467/use-coredata-or-sqlite-on-iphone) – Brad Larson Feb 23 '12 at 18:50
  • Core Data works well on multiple threads. See the WWDC 2011 sessions in particular Optimizing Core Data Applications. Each thread must have it's own managedObjectContext and then must be changes merged. This sounds drastic, but is actually quite easy once you get into core data. – SpaceTrucker Mar 15 '12 at 00:58

2 Answers2

2

In my opinion, Core Data would be a better option for this particular case, particularly with the relationships you have between the appointments, customers, and employees.

Core Data is more than just a database, it's an object graph management system. This means that you can easily maintain your relationships of customer, employee, and appointments by specifying these in your data model as one-to-one, one-to-many, or many-to-many, depending on what is appropriate. Assigning an appointment to an employee can be as simple as setting that assignment's employee relationship to the appropriate object, or doing the inverse of adding an appointment to an appointments relationship within the employee. Fetching these appointments is as easy as grabbing the objects in that employee's relationship, and the code for this can be made very clean by the use of custom NSManagedObject subclasses with matching properties for these relationships.

As SteAp points out, it's easy to write NSPredicates to filter based on a certain time range as explained in this question. By using the appropriate options, you can run your application in a debug logging mode to see the SQLite queries produced in response to these predicates, and they're usually what you would have written to achieve the same fetch.

On iOS, Core Data almost always uses SQLite as its backing data store, so you would think that it would introduce significant overhead on top of this. To the contrary, it performs some pretty good optimizations such as batched fetching that can cause Core Data to outperform raw SQLite in many cases. I've witnessed this within my own applications, particularly when it comes to long lists of results that you want to present in a table or a similar structure. There are certain cases where it breaks down, but those are few and far between.

I've done both raw SQLite and Core Data implementations of data models in my applications, and in every case Core Data has dramatically reduced the amount of code I've needed to write. Combined with the performance advantages I've seen, I have moved or will be moving all of my SQLite-based projects to Core Data.

Even Brent Simmons, who wrote the above-linked article about a specific performance issue that caused him to stay away from Core Data in one case, has this to say:

I bet Core Data is the right way to go 95% of the time. Or more. It’s easy to work with. It’s fast (in most cases).

and he followed that up with the more recent statement:

This may come as a surprise to people who have it in their head that I’m the guy who doesn’t like Core Data. So I’ll say this — on top of all the other good reasons to use Core Data, here’s another: Core Data is the standard Cocoa object persistence system.

When it gets better, our apps get better. (And it does keep getting better.)

And, more importantly, as the standard it means that any Cocoa developer — a teammate or someone who acquires the app — can jump in and quickly understand how it works.

Yes, your custom thing may be better for your app. But will it stay better? And if you bring someone on to help you, how quickly will they learn your custom thing?

Use Core Data.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Can you look at my update for the OP and let me know your thoughts? – Bot Feb 23 '12 at 19:20
  • @jostster - Multithreaded access to Core Data is tricky, but so is multithreaded access to base SQLite. People use multithreaded access to Core Data all the time, and Apple has a significant amount of documentation around this: https://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html . There have also been some significant enhancements in iOS 5.0 regarding how multithreading is handled around Core Data contexts: https://developer.apple.com/library/ios/#releasenotes/DataManagement/RN-CoreData/_index.html#//apple_ref/doc/uid/TP40010637 . – Brad Larson Feb 23 '12 at 20:03
  • If I am using `dispatch_async` to fetch data from my api then storing that into core data would that be considered multi threaded? Would that be allowed with core data? – Bot Feb 23 '12 at 20:10
  • @Computer - Yes, by dispatching a block on a background queue you are operating in a multithreaded manner. The new additions to Core Data in iOS 5.0 and Lion make it easier to interact with GCD, as I point out above. You can do this, as long as you take appropriate precautions. – Brad Larson Feb 23 '12 at 21:37
  • Why not create ONE queue for all CoreData operation and put all blocks in this single CD queue? This way, CoreData runs concurrent regarding the app's main code - but not concurrent regarding 'itself'. – SteAp Feb 23 '12 at 21:58
  • @BradLarson A more general answer: I like (software) technology that fits a job and allows for good conceptual design. That said, Core Data is just one of many technologies. Quite likely, Core Data isn't a second class citizen in the Apple world ;-) – SteAp Feb 23 '12 at 22:05
0

For CoreData, I'd propose to learn from this Apple tutorial.

In general, I'd propose CoreData, since it is well integrated with Interface Builder. E.g. using bindings, you can

  • enable buttons
  • show or hide control
  • bind a subordinate NSTableView to he selection of its parent NSTableView (by array controllers)

Thus, I strongly propose to use Core Data.

If your app manages just a bunch of small items, why not put them in the user defaults database.

In case you'd like to support iCloud too, CoreData is the way to go. Have a look at WWDC 2011 Session 315 or review this tutorial: iOS How-To : Using Core Data with iCloud.

BTW, nib2objc is a nice project to translate XIBs to functional equivalent ObjC code.

SteAp
  • 11,853
  • 10
  • 53
  • 88
  • Is it easy for core data to filter by date ranges and employees without having to loop through all the data? – Bot Feb 21 '12 at 23:03
  • 1
    @jostster See this post: http://stackoverflow.com/questions/1965331/nspredicate-filtering-objects-by-day-of-nsdate-property – SteAp Feb 22 '12 at 23:03
  • Me? Yes, I generally like core data. What I don't like IB's interface to bindings. All those ultra small controls / text fields to set... – SteAp Feb 23 '12 at 21:52