Search Your Question

Showing posts with label CoreData. Show all posts
Showing posts with label CoreData. Show all posts

What is Core Data Stacks?

Ans : Core Data is Apple’s object graph management and persistency framework.

Core Data Stack
Core Data Stack

  1. Managed Object
  2. Managed Object Context
  3. Managed Object Model
  4. Persistent Store Coordinator

  1. NSManagedObject’s are the model objects exposed by Core Data.
  2. NSManagedObjectModel is a database schema that describes the application’s entities. It defines the structure of the managed objects.
  3. NSPersistentStoreCoordinator associates persistent storage and managed object model. It lends itself to mapping the data from the storage, say SQLite data base rows, into the object model. It is a task of high complexity and is often taken for granted when working with Core Data.What is more, the persistent coordinator is used by the managed object context when it comes to saving or fetching objects.
  4. NSManagedObjectContext controls the life cycle of the managed objects and provides operations to create, save and fetch them. You can think of the managed object context as a scratch pad with a group of managed objects.

Starting from iOS 10, NSPersistentContainer is responsible for creation and management of the Core Data stack.
NSPersistentContainer exposes a managed object model, a managed object context and a persistent store coordinator as well as provides many convenience methods when working them, especially when it comes to multithreaded applications.

What is delete rule in Core Data?

Ans : In our database, there may be multiple entities. One entity is connected with another entity in relationship. So deleting one data from one entity may have impact on another entity data depends on relationship or delete rule.

There are 4 delete rules :

  1. No Action
  2. Nullify
  3. Cascade
  4. Deny
Let's take example having one department entity and it is connected to employee entity with relationship as one-to-many. Employee to department entity relationship is one-to-one.

1. No Action : When No Action delete rule is set to relationship, then there will be no impact of deleting record from one entity. If we delete record from department connected to multiple employees, then there be no impact on employee entity. Employee assume that it has still relation with deleted department.

2. Nullify : If delete rule is set to Nullify to the relationship then the destination of the relationship gets nullify.  In our case, if department is deleted, then relationship between employee and department gets nullify. This is default delete rule.

3. Cascade Rule : If we delete department, then its related employees will be deleted if this delete rule is set. This rue is only used when data model has more dependency. 

4. Deny Delete Rule : This is opposite of Cascade Rule. If we set this rule, and we try to delete department which is connected to any of employee, then we are not allowed to delete department.

Depends on project requirement, we can set delete rule.