Monday 12 March 2018

Initializing Core Data In Swift 4 For Mobile App Development


Core Data in Swift 4 


Why Core Data?

There are multiple ways to store data in your iOS app but one of the best way is to use core data . also allows you to store your data within app bundle and your data to be retained on app launches.

What’s Core Data?

According to Apple ,Mobile App Development for Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
Long story short ,Core Data is a framework for managing an object graph.

Important things to remember

       Core Data is not a relational Database
       Core Data uses SQLite as its default persistent store
       Core Data purely model layer framework
       Core Data manages Object Graph



Initializing Core Data Stack

We will see one sample project to understand the core data stack before that we should remember some of the primary objects of core data stack mentioned below

       Managed object context (NSManagedObjectContext)
       Persistent store coordinator (NSPersistentStoreCoordinator),
       Managed object model (NSManagedObjectModel)
       Persistent container (NSPersistentContainer)

Lets see what’s this all about

Managed object context
Managed Object Context is the one you’ll work with for most of the time. normally, whenever you need to fetch and save objects in persistent store, the managed object context is the first component you’ll talk to.
Managed Object context play a vital role in fetching the objects from persistent store, modifying those objects as we like and save those changes back to persistent store.
All managed objects must be registered with managed object context so that managed objects context keep track of any changes made to those objects.By tracking changes, the context is able to provide undo and redo support.

Persistent store coordinator

Like I mentioned above SQLite is the default persistent store in core data. The NSPersistentStoreCoordinator sits in the middle of the Core Data stack. with the help of core data we can setup multiple persistent store with different entities. The Persistent Store Coordinator is responsible to manage different persistent object stores and save the objects to the stores.

Managed object model defines the schema of the data, the store coordinator is fully aware of objects from the data in the persistent store and passed those objects from persistent store to requesting managed object context.


Managed Object Model

Managed Object Model is acts like database schema and its represented by a collection of entities. In Xcode, the Managed Object Model is defined in a file with the extension .xcdatamodeld.


Persistent container

Starting in iOS 10 and macOS 10.12 Apple added a new class NSPersistentContainer to core data. This class handles the creation of coredata stack and offers access to the NSManagedObjectContext as well as a number of convenience methods.


Conclusion
That’s all about the core data basics theory part, In the next tutorial we can see how to create and store the data in persistent store with detailed swift code.

  Author

 Adarsha Upadhya 

Canarys Automations Pvt Ltd







No comments:

Post a Comment