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?
- 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
- 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 anSQLite 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?
- NSManagedObjectModel.
- NSManagedObjectContext.
- NSPersistentStoreCoordinator.
- 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 amanagedobject
. 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 theNSPersistentStoreCoordinator
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 theMain 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 byNSMainQueueConcurrencyType
andNSPrivateQueueConcurrencyType
.
What is the Faulting mechanism in Core Data?
- 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.- 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 itswillAccessValueForKey
: 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
No Action
- If I add this delete rule on arelationship
and then delete one of theperson
then it will not do anything with the task but theperson
got deleted. The task still points to the person that we deleted.Nullify
- If I apply this delete rule and delete theperson
then associate tasks will point to the nullperson
. For example, I deleted the thor and thor tasks will point to the null person.Cascade
- In this rule, if I delete the person then it willdelete all
the tasks associated with that person.Deny
- Now we haveperson
i.e.IronMan
with 2 tasks. In this rule, if I try todelete 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 thePerson
.
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 amanagedobject
reference from one queue to another,
Note: It must be done through
NSManagedObjectID
instances
.You retrieve themanagedobjectID
of amanagedobject
by calling the objectID method on theNSManagedObject
instance.
What are the minimum necessary classes and the relationship between them?
We need
NSManagedObject
,NSManagedObjectContext
andPersistenceStoreContainer
. These classes/objects are themain building blocks
of the core data stack. Data operations likeinsert
,update
anddelete
are performed inmanagedobjeccontext
.Managedobjectcontext
can contain one or moremanagedobjectmodel
instances.PersistenceStoreCoordinator
communicates betweenmanagedobjectcontext
and store.
What is Uniquing?
CoreData
ensures that — in a givenmanagedobjectcontext
— an entry in aPersistentStore
is associated with only onemanagedobject
. The technique is known asUniquing
withoutuniquing
, 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 inCoreData
. The classic example is anEmployee
or aCompany
. In arelational database
, an entity corresponds to atable
. An attribute is a piece of information attached to a particular entity. For example, anEmployee
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.