1

i am new to this Core data. When i search for tutorials, I'm seeing this sentence Core Data is not a database everywhere on the internet.

If it is not a database, why are we using it as a database?

For what purpose Core Data is initially created?

Is there any other way Core Data was used before/will be used in future (Other than as a DB)?

Sorry for my English.

Thanks for the time.. (:

Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • http://stackoverflow.com/questions/523482/core-data-vs-sqlite-3 – Adithya Aug 25 '16 at 09:24
  • 1
    it depends on your definition of the word database - it is used to store data, probably you don't want to worry about the definition too much at this stage... – Wain Aug 25 '16 at 09:24
  • http://stackoverflow.com/questions/26599599/core-data-is-not-a-relational-database-why-exactly-is-this-important-to-know – Woodstock Aug 25 '16 at 09:25
  • @Adithya I'm not asking the difference between sqlite and coredata.. I am just talking about core data alone – Ganesh Aug 25 '16 at 09:26
  • @Woodstock They are talking more about **relational database** not about database or what i am expecting – Ganesh Aug 25 '16 at 09:28
  • @Wain I don't want to simply accept the fact that CoreData is not a database but we will use it for storing data. I just want to make it clear. Correct me if i am wrong. – Ganesh Aug 25 '16 at 09:30
  • To my knowledge, if we directly use the sqlite with SQL queries for fetching and inserting the data to database, the query may not be that much optimised. Core data is the set of API provided by apple so that you don't want to worry about how optimised your query is and how faster it will retrieve or insert data. Behind the scene, apple will generate the most optimised queries for your save and execute fetch request functions (sorry for the function name, not exactly remembering). – Sreekanth Aug 25 '16 at 09:31
  • @Ganesh Please go through accepted answer in the link that I shared. You will learn a lot. For the basic question that you are asking, you cannot expect someone to post a comprehensive answer, when its already done and discussed. – Adithya Aug 25 '16 at 09:31
  • @Adithya It was clear. I read it fully. But it doesn't answer for my 2nd and 3rd questions. If it is answering, I am sorry that i didn't noted. Can you tell me that ? – Ganesh Aug 25 '16 at 09:47

3 Answers3

2

If it is not a database, why are we using it as a database?

"We" are not necessarily doing so, depending on who you mean by "we". Core Data can be used in a database-like manner, keeping in mind the Core Data vs. SQL differences others have noted. But that's not the only possible use.

Statements that Core Data isn't a database are mostly intended to prevent people from thinking of Core Data in the same sense as SQL. That sort of thinking leads to badly designed data models, since the approach is different. It can be used as a database in the generic sense of the storing structured data but it's important to not assume that it works like other databases you may have used.

For what purpose Core Data is initially created?

Is there any other way Core Data was used before/will be used in future (Other than as a DB)?

Core Data was created to fill what might have been perceived as a missing piece in Apple's frameworks. They generally take the MVC approach. There were classes to help in designing and implementing views (in these pre-iOS days that meant AppKit) and with controller classes (for example NSArrayController, also an OS X class). But model support was limited to NSCoding, which required a lot of repetitive code. Custom solutions also had trouble scaling to large amounts of data-- for example NSCoding doesn't help you load only part of a large document graph, because it recursively works its way through the entire object hierarchy.

Core Data was added with the purpose of making it easier to design and implement the model layer of an app. It's no accident that the document you edit to design the data is called a data model and not a schema.

The idea was (and is) that you could

  • Design your data model visually in Xcode
  • Create and use instances of your model objects
  • Read and save those objects in a file

...all without ever needing to write your own code to figure out how to convert your model objects to and from something that could be written into a file, or with the mechanics of opening a file, reading/writing it, and saving it. You'd just create objects as needed and then call save on NSManagedObjectContext. The small bit of code that was concerned with locating and opening the file was all pretty much the same in any app, so while it was still required it was mostly no longer the app developer's concern (and in iOS 10, NSPersistentContainer eliminates even this). You'd also get the benefit of only needing to load the parts of your object graph that you currently needed instead of loading everything every time.

As you've noticed, in practice Core Data is commonly used more or less like a database, and it works for that. But it's not designed to be limited to such uses.

Community
  • 1
  • 1
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • This really make sense.. But i have some doubts, what is the need of NSCoding (Saving Process) in Model Class these days... I have searched in Internet how model class was used before but i didn't catch the point what you are saying... – Ganesh Aug 29 '16 at 05:29
  • You said it is limited to NSCoding.. I assume some saving purpose is needed in Model Class. If so, we could have used SQLite in Model Class.. Why NSCoding or (Core Data)? (Honestly, i don't know why you need. Please correct me if my question is wrong.. – Ganesh Aug 29 '16 at 05:33
  • To my knowledge, Model represents the data that your app is operating with. It doesn't necessarily have to do anything with data persistence (Saving) . So In what case we need data persistense in Model ? – Ganesh Aug 29 '16 at 05:54
  • A model without persistence is only useful if you never need to save your data. Most apps need to save data, which is why persistence is important. – Tom Harrington Aug 29 '16 at 16:11
  • why don't we use sqlite instead of saving data as object – Ganesh Aug 29 '16 at 16:13
  • Because you don't have to convert your objects to/from table rows. Because you get to use relationships to load related objects automatically instead of doing still more SQL calls and requiring foreign keys. Many other reasons. [Take a look at the docs](https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/CoreData/) and notice how different it is from using SQLite directly. – Tom Harrington Aug 29 '16 at 16:17
  • Well said.... Now I got some idea about it.... What I understood is, its not a database... But for data persistent (Saving data as object) it is used. am I right? If yes, how can we use core data which doesn't involve in data persistent concept.? – Ganesh Aug 30 '16 at 01:58
  • Stack Overflow comments are not a good place for an ongoing discussion. Take a look at Apple's Core Data documentation, which answers your latest question. If it's still not clear, ask a new question here. And if I have answered your original question, please consider accepting it. – Tom Harrington Aug 30 '16 at 04:21
  • Actually my latest question is the real question in which we are commenting... That's what I am asking from the start... Your answers were really great and helped me to understand why CD is used in Model instead of SQLite... Thank you so much for spending your time and giving such brief description. But my real question is usage of core data other than a database. – Ganesh Aug 30 '16 at 04:35
1

Actually Core Data is a bridge between the code and the underlying database (like SQLite or XML...). Core Data is not a database, but uses SQLite (or XML...) for persistence. The main purpose of Core Data is to manage memory objects and object graphs easily without having to do it manually through a SQLite library for instance. It is possible to use Core Data without persistence is you want (using In-Memory stores).

Here is the documentation : https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/index.html#//apple_ref/doc/uid/TP40001075-CH2-SW1

Bye!

tdimeco
  • 856
  • 7
  • 12
  • You said **The main purpose of Core Data is to manage memory objects and object graphs easily without having to do it manually** . Hence we are using it's feature to easily work with SQLite... Can we use that feature for other purpose ? Or We can only use it for SQLite purpose. – Ganesh Aug 25 '16 at 09:39
  • ** It is possible to use Core Data without persistence is you want.** Can you tell me an example for this so that i can understand more clearly.. (: – Ganesh Aug 25 '16 at 09:41
  • Can coredata act as a bridge between the code and any other thing except database ? – Ganesh Aug 25 '16 at 09:52
  • Sure, Core Data helps you managing your data in an object oriented way (you can create objects in you code, make relations between them and fetch them using requests). It's not strongly related to SQLite. You can use XML as storage and continue to manipulate your objects as usual. Actually, by default, Core Data can use 4 types of storage : SQLite, XML, Binary and In-Memory. If you use the In-Memory type, there is no persistence on disk, but you can continue to manipulate objects by code (but when your app will terminate, data will be lost in this case). – tdimeco Aug 25 '16 at 09:55
  • I'm sorry. You are still talking about the storage (Database). I'm asking it's usage other than a Storage (Database) . @tdimeco – Ganesh Aug 26 '16 at 08:29
  • SQlite can also be in-memory only. I say there is almost never a reason to use Core Data. – Jonny Jan 26 '23 at 13:03
0

Yes it is true , Core data is not a Database, though internally it saves data using sqlite. We use Coredata for persistent data which means we should be able to save the data and use it even after closing and reopening the app. There are various ways to store data like Sqlite,Plist,UserDefaults and CoreData. Coredata does not maintain any relations like SQlite. It means there are no keys like primary and foreign etc. Coredata allows you to deal with data in Object Oriented Approach. You can easily work with data operations even you don't have knowledge about DB queries.

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
  • Okay...You said "internally it saves data using sqlite". My question is , is Coredata created only for persistent data ( Which actually means storing the data )? If yes, then why don't we call it as a database. In other words, i can as what are the usage of core data other than a database ( persistent data, storing data , etc ) – Ganesh Aug 25 '16 at 09:34
  • please refer http://www.cocoawithlove.com/2010/02/differences-between-core-data-and.html and http://www.drdobbs.com/mobile/ios-data-storage-core-data-vs-sqlite/240168843 for more clarification. – Vishal Sonawane Aug 25 '16 at 09:36