5 ms·
Should you use CoreData?
- nephorider 13y agoVery true article. CoreData has a lot of path and legacy behind and thinking it would solve some problem magically because it is vended as "the high level problem solver" is a mistake. More than once I have seen performance problem appearing 2 weeks before release by (mis)using it.
- anandabits 13y agoAs with any technology, CoreData works well when the problem domain falls within its sweet spot. When the problem domain is outside that, CoreData can quickly become a challenge to work with.
- zoul 13y agoWhile this is true, the problem is that the sweet spot for Core Data is quite small. Given the framework description, it’s quite easy to go with Core Data when some of its many drawbacks mean you really shouldn’t.
- mmorey 13y agoIf you plan on using Core Data please read objc.io issue #4[1]. The reason I use Core Data is because the alternatives for data persistence are not that great. We basically have NSCoding Protocol[2] and FMDB[3]. I find both cumbersome to use. Also, not that it matters for this conversation but it is Core Data, not CoreData. [1] http://www.objc.io/issue-4/editorial.html http://www.objc.io/issue-4/editorial.html [2] https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/... [3] https://github.com/ccgus/fmdb https://github.com/ccgus/fmdb
- mpweiher 13y agoobjc.io issue 4 actually demonstrates one of the issues with Core Data: if you check the code, you see that it is not modeling the domain, instead it is encoding interactions with the database. Sigh.
- mmorey 13y agoAre you referring to the full Core Data application[1]? Can you give an example of what you mean by "encoding interactions with the database"? [1] https://github.com/objcio/issue-4-full-core-data-application https://github.com/objcio/issue-4-full-core-data-application
- skue 13y agoI'm curious why you find FMDB cumbersome? I've always found it pretty easy to work with.
- tonyarnold 13y agoI find it cumbersome because it's as much work as Core Data and seems to have many of the same API choices around threading, but I also have to write SQL. I don't like writing SQL.
- programminggeek 13y agoKnowing when to use and when not to use a tool is as important as knowing how to use a tool. That is why before learning the easy/magic frameworks I try and do things by hand so I at least understand what the tradeoffs are and what that framework is actually providing. I think a better explanation in this article would be to not use CoreData until you have hit the particular pain point that CoreData solves. You are almost always better off making a tradeoff that you understand than one that you don't.
- sjtgraham 13y ago> It's an older article (2012), but recently came to my attention via Drew McCormack (@drewmccormack): "Great post", he wrote, and after reading the article I not just disagreed, but found that Twitter wasn't really adequate for writing up all the things wrong with that article. This is how I discovered the article and also felt exactly the same way as OP. It was a massive amount of hand-waving, appeal to authority, and supposition.
- biznickman 13y agoUltimately you need some way of archiving/caching data and Core Data is a great way of doing this. I started off with NSArchiver/NSCoding until I found MagicalRecord which made CoreData infinitely easier for me. Add MR to your project and never look back!
- numo16 13y agoI moved from straight CoreData to ObjectiveRecord and haven't looked back. There are so many excellent solutions[0] out there that make CoreData much easier to use that I can't see much reason to using CoreData directly any more. [0]:http://nshipster.com/core-data-libraries-and-utilities/ http://nshipster.com/core-data-libraries-and-utilities/
- mikeash 13y agoThe short and easy answer is no, never. Core Data is just not a good technology. People on the other side of this debate will say that there's an appropriate time for everything, but nothing makes sense for everything, so you have to choose based on your needs. This is half true. It misses the fact that some things are just plain bad and are never better than the alternatives in any situation, and IMO Core Data is in this category. There are several fundamental problems with it. First, the API is awful. If you want decent model objects in memory you either have to do a bunch of manual work, or use a third-party tool like mogenerator. Even then, the result is a massive soup of mutable objects with no intelligence. The structure of the API encourages passing the entire context around everywhere, which basically turns all of these objects into global variables. Look no further than popular Core Data wrapper APIs to see how bad this can get. For example, ObjectiveRecord[1] adds class methods for looking up objects using a predicate. Modularity? Separation of concerns? Perish the thought. Second, it ties your on-disk representation to your in-memory representation way too strongly, and this makes it more difficult to choose appropriate structures for either one, and more difficult to make changes to either one. Third, it locks you into the technology something fierce. Core Data is so different from everything else that once you build your model layer on it, you're just about stuck there forever. You can move away, but it's a ton of work that reaches into every corner of your app. Other solutions, like serializing to a property list or SQLite, simply require a translation layer that you can switch out more or less at will. Fourth, it's unbelievably slow. Literally unbelievable, as in I tell people about it and they don't believe me. It's too slow for a large number of records. It's fast enough for a very small number of records, but at that level it provides no advantage over using property lists and just loading everything into memory. At the large scale, you don't want to load everything into memory so you need a better scheme, but then you need something like SQLite so you can avoid the slowness and gain more control. Core Data is really only workable when your quantity of data is in a happy medium, which exists roughly within the range of 1097 to 1143 records. (Warning: previous numbers made up.) Since you can almost never guarantee that your data will live in that happy medium and not exceed it, Core Data is a bad choice. I understand why its proponents advocate for it. It's shiny. Apple pushes it hard, and people tend to like what Apple tells them to like. It gives the illusion of making things easy. It does make certain simple things easy. The problem is that making easy things easier is not a virtue. Making difficult things easier is what counts, and Core Data fails at that. [1] https://github.com/mneorr/ObjectiveRecord https://github.com/mneorr/ObjectiveRecord
- skue 13y agoMost iOS devs fail to realize that CoreDate was originally created for managing data in desktop apps, but many of the advantages it offers for Mac development don't exist on mobile. For example, iOS has no bindings. On the Mac you can bind your UI elements to data objects backed by Core Data so that as the data changes everything stays in sync. (Yes, like modern JS libraries.) For simple use cases you can do a surprising amount of development with very little code, although for complex use cases it can sometimes get ugly. Also, I believe an SQL-based approach is more appropriate for the smaller screen sizes with iOS. The master-detail approach on iOS typically works out to showing a master list with a few labels describing each object, then drilling into to the object's details. In many cases all the labels on a master list (plus an associated object id) can fit into a single in-memory array, and an SQLite query works great to pull these labels strings off disk when the view loads. There's little reason to have CoreData manage faulting/loading entire data objects as users scroll up and down the master list just to view the titles. Of course the SQL data access should still be abstracted, but CoreData is overkill in cases like this.