Core Data iOS - Imtiaz211/interviews GitHub Wiki

Core data and NSKeychain Understanding

Keychain is perfect to store secret values (API keys, password, …) with security. All data stored in Keychain is encrypted.

What is Core Data?

Coredata is an object graph manager which also has the ability to persist object graphs to the persistent store on a disk. An object graph is like a map of all the different model objects in an iOS application.

Difference between SQlite and Core Data?

  1. SQLite - Have Data Constraints feature. - Operates on data, stored on disk. - Can Drop table and Edit data without loading them in memory. - Slow as compared to core data. - It is database
  2. Core Data - Don't have Data Constraints, if required, need to be implemented by business logic. - Operates on in memory.(data needs to be loaded from disk to memory) - Need to load the entire data if we need to drop tables or update. - Fast in terms of record creation(saving them may be time consuming) - ORM(Object Relational Model)

Note: Additionally apart from SQLite as back-end Core data can use XML or binary format for storing data to disk.

What is NSFetchRequest?

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, individual values and more.

Explain NSPersistenContainer?

PersistentContainer creates and returns a container, having loaded the store for the application to it. By default, Core Data uses an SQLite database as the persistent store. The first step is to create a managed object model.

What is an NSManagedObject?

NSManagedObject represents a single object stored in Core Data. It is a shape-shifter. it is a runtime representation of a Core Data entity. You can read and write to its attributes using Key-Value Coding.

What is NSManagedObjectContext?

ManagedObjectContext represents a single object space, or scratch pad, in a Core Data application. Its primary responsibility is to manage a collection of NSManagedObjects`

What is data persistence?

DataPersistent in the field of data processing denotes information that is infrequently accessed and not likely to be modified.

What is the core data stack?

  1. NSManagedObjectModel.
  2. NSManagedObjectContext.
  3. NSPersistentStoreCoordinator.
  4. NSPersistentStore (storage).

What types of stores does core data support?

CoreData offers native file types for a persistent store:- Binary, XML, and SQLite.

What is Entity Inheritance?

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. As long as a class descends from NSManagedObject and responds to the right key-value messages for the entity it represents.

What is an abstract entity in core data?

An Entity can be abstract, in which case it is never directly attached to a managedobject. if you will not create any instances of that entity. You typically make an entity abstract.

What is a FetchedResultscontroller?

A controller that you use to manage the results of a CoreData fetchrequest and display data to the user in UITableView.

What is PersistentStore?

A persistentstore is a repository in which managed objects may be stored.

What is NSPersistentStoreCoordinator?

An object persists data to disk and ensures the persistent store(s) and the data model are compatible. It mediates between the persistent store(s) and the managed object context(s) and also takes care of loading and caching data. That’s right. Core Data has caching built-in. PersistentStoreCoordinator is actually the instance of the NSPersistentStoreCoordinator class.

Can we do Multithreading with core data?

We can do multithreading in core data with multiple contexts, for example background context for long-running tasks (batch inserting/fetching/updating) and update UI on the main thread.

Multithreading in Core data?

Core Data, Multithreading, and the Main Thread In Core Data, the managed object context which is the heart of the Core Data stack can be used with two concurrency patterns, defined by NSMainQueueConcurrencyType and NSPrivateQueueConcurrencyType.

What is the Faulting mechanism in Core Data?

  1. When you retrieve an object from an NSManagedObjectContext (MOC) you can’t tell (in the normal course of its use) whether it’s a fault or a realized object.
  2. A fault will be converted into a realized object (“fired”) automatically by the Core Data framework in most cases when it is necessary to do so, For example:- when accessing a property of the object. If you need to fire a fault yourself, you can do so by invoking its willAccessValueForKey: method with a nil argument. — Yes we can, use –isFault

What is NSManagedObjectModel?

it contains information about the models or entities of the object graph, what attributes they have, and how they relate to one another. It knows about the data model by loading one or more data model files during its initialization.

Delete Rule

  1. No Action - If I add this delete rule on a relationship and then delete one of the person then it will not do anything with the task but the person got deleted. The task still points to the person that we deleted.
  2. Nullify - If I apply this delete rule and delete the person then associate tasks will point to the null person. For example, I deleted the thor and thor tasks will point to the null person.
  3. Cascade - In this rule, if I delete the person then it will delete all the tasks associated with that person.
  4. Deny - Now we have person i.e. IronMan with 2 tasks. In this rule, if I try to delete Iron Man then it does not allow me to do. It gives the error _The operation couldn’t be completed_. (Cocoa error 1600.)" on saving the context. Now to delete the Iron Man person, we need to delete all his associated tasks and then we could able to delete the Person.

What is an NSManagedObjectId?

NSManagedObject instances are not intended to be passed between queues. Doing so can result in corruption of the data and termination of the application. When it is necessary to hand off a managedobject reference from one queue to another,

Note: It must be done through NSManagedObjectID instances.You retrieve the managedobjectID of a managedobject by calling the objectID method on the NSManagedObject instance.

What are the minimum necessary classes and the relationship between them?

We need NSManagedObject, NSManagedObjectContext and PersistenceStoreContainer. These classes/objects are the main building blocks of the core data stack. Data operations like insert, update and delete are performed in managedobjeccontext. Managedobjectcontext can contain one or more managedobjectmodel instances. PersistenceStoreCoordinator communicates between managedobjectcontext and store.

What is Uniquing?

CoreData ensures that — in a given managedobjectcontext — an entry in a PersistentStore is associated with only one managedobject. The technique is known as Uniquing without uniquing, you might end up with a context maintaining more than one object to represent a given record.

Core Data comes with its own vocabulary?

An entity is a class definition in CoreData. 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 have a to-one relationship with his manager.