What is Core Data in Swift ?
core data interview questions
Core data allows the developer to interact with the model layer in object oriented manner. It manages the object graph.
Object graph is the collection of object. Core data framework takes the care of managing the objects life cycle in object graph.
What are the Types of stores in core data
Benefits of Core Data
- Core data is default Apple framework to store data locally.
- it is not the third party dependency, thus no need to worry about managing it .
- It manages the data using object oriented graph. no need of data Management like insert, delete or save
- Core Data take care of life cycle of the object in the object graph.
Drawbacks of Core data.
- Core data is the framework not the database. It depends on Sql Database to store the data.
- We needed to think data as Object oriented graph.
- It is not the SQL Wrapper.
How Does core data stores the Data?
Core Data save the Data in the form of persistence Data .
The core data stack Contain the 4 segment.
NSManagementObjectContext : It manages the object context and the collection of Object model.Each managed object context is backed by persistent store Coordinator . It is also Known as managed object context.
NSManageObjectModel: It is also a managed object model. it contains the information about the model or entites of the object graph
NSPresistentStoreCoordinator: Its called as A persistent store Coordinator. A Object persists data to disk and ensure the persistent store and the data model are compatible . It act as mediator between the Data model and persistent store
NSPresistentContainer: It s known as a persistent Store(storage). Its a repository in which managed object will be stored. Core data NSPresistentContainer stores the file in the XML,binary and SQLite
What is FetchedResultController ?
FetchedResultController is the controller is used to manage the data from the core Data using coreData fetch Request and display to the screen in UITableView.
How to transfer manage object from from one Context(thread) to another ?
Each thread(Context) must have the own object context and object model . We can transfer object using the NSManagedObjectID.
Do NSPersistentStoreCorordinor can have more persistent Store ?
Yes but needed to have relationShip between the object in different store.
How to Handle Core Data with swift
The first step is to create a managed object model, which describes the way Core Data represents data on disk.
By default, Core Data uses a SQLite database as the persistent store, so you can think of the Data Model as the database schema.
• An entity is a class definition in Core Data. The classic example is an Employee or a Company. In a relational database, an entity corresponds to a table.
• An attribute is a piece of information attached to a particular entity. For example, an Employee entity could have attributes for the employee’s name, position and salary. In a database, an attribute corresponds to a particular field in a table.
• A relationship is a link between multiple entities. In Core Data, relationships between two entities are called to-one relationships, while those between one and many entities are called to-many relationships. For example, a Manager can have a to-many relationship with a set of employees, whereas an individual Employee will usually have a to-one relationship with his manager.
Core Data with Swift
Core Data, an attribute can be of one of several data types. Set the new attribute’s name to, er, name and change its type to String:
NSManagedObject represents a single object stored in Core Data; you must use it to create, edit, save and delete from your Core Data persistent store. As you’ll see shortly, NSManagedObject is a shape-shifter. It can take the form of any entity in your Data Model, appropriating whatever attributes and relationships you defined. This is where Core Data kicks in! Here’s what the code does:
1 Before you can save or retrieve anything from your Core Data store, you first need to get your hands on an NSManagedObjectContext. You can consider a managed object context as an in-memory “scratchpad” for working with managed objects.
Think of saving a new managed object to Core Data as a two-step process: first, you insert a new managed object into a managed object context; then, after you’re happy with your shiny new managed object, you “commit” the changes in your managed object context to save it to disk.
Xcode has already generated a managed object context as part of the new project’s template. Remember, this only happens if you check the Use Core Data checkbox at the beginning. This default managed object context lives as a property of the NSPersistentContainer in the application delegate. To access it, you first get a reference to the app delegate.
2 You create a new managed object and insert it into the managed object context. You can do this in one step with NSManagedObject’s static method: entity(forEntityName:in:).
You may be wondering what an NSEntityDescription is all about. Recall earlier, NSManagedObject was called a shape-shifter class because it can represent any entity. An entity description is the piece linking the entity definition from your Data Model with an instance of NSManagedObject at
3 With an NSManagedObject in hand, you set the name attribute using key-
value coding. You must spell the KVC key (name in this case) exactly as it
appears in your Data Model, otherwise your app will crash at runtime.
4 You commit your changes to person and save to disk by calling save on the
managed object context. Note save can throw an error, which is why you call it using the try keyword within a do-catch block. Finally, insert the new managed object into the people array so it shows up when the table view reloads.
1 Before you can do anything with Core Data, you need a managed object context. Fetching is no different! Like before, you pull up the application delegate and grab a reference to its persistent container to get your hands on its NSManagedObjectContext.
2 As the name suggests, NSFetchRequest is the class responsible for fetching from Core Data. Fetch requests are both powerful and flexible. You can use fetch requests to fetch a set of objects meeting the provided criteria (i.e. give me all employees living in Wisconsin and have been with the company at least three years), individual values (i.e. give me the longest name in the database) and more.
Fetch requests have several qualifiers used to refine the set of results returned. For now, you should know NSEntityDescription is a required one of these qualifiers.
Setting a fetch request’s entity property, or alternatively initializing it with init(entityName:), fetches all objects of a particular entity. This is what you do here to fetch all Person entities. Also note NSFetchRequest is a generic type. This use of generics specifies a fetch request’s expected return type, in this case NSManagedObject.
3 You hand the fetch request over to the managed object context to do the heavy lifting. fetch(_:) returns an array of managed objects meeting the criteria specified by the fetch request.
When we talk about persistent data, people probably think of database. If you are familiar with Oracle or MySQL, you know that relational database stores data in the form of table, row and column, and it usually facilitates access through what- so-called SQL query. However, don’t mix up Core Data with database.
Though SQLite database is the default persistent store for Core Data on iPhone, Core Data is not a relational database. It is actually a framework that lets developers store (or retrieve) data in database in an object-oriented way. With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL.
What are relationships in core data?
Managed Object Model – It describes the schema that you use in the app. If you have a database background, think of this as the database schema. However, the schema is represented by a collection of objects (also known as entities). In Xcode, the Managed Object Model is defined in a file with the extension .xcdatamodeld.
You can use the visual editor to define the entities and their attributes, as well as, relationships.
Persistent Store Coordinator – SQLite is the default persistent store in iOS. However, Core Data allows developers to setup multiple stores containing different entities.
The Persistent Store Coordinator is the party responsible to manage different persistent object stores and save the objects to the stores. Forget about it you don’t understand what it is. You’ll not interact with Persistent Store Coordinator directly when using Core Data.
Managed Object Context – Think of it as a “scratch pad” containing objects that interacts with data in persistent store. Its job is to manage objects created and returned using Core Data. Among the components in the Core Data Stack, the Managed Object Context is the one you’ll work with for most of the time. In general, whenever you need to fetch and save objects in persistent store, the context is the first component you’ll talk to.