Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an the function of AppDelegate and Context in swift?

I just started working with core data in iOS app dev using swift. The first two things that I encountered are: 1. AppDelegate 2. NSManagedObjectContext.

  1. I understand that 'AppDelegate.swift' file is a source file just like the 'ViewController.swift'. But in all the tutorials it was referred as 'something which will be used later'. Perhaps, now is the time to get familiar with it. Could you kindly tell me what exactly it does?

  2. What does an object of type 'NSManagedObjectContext' do? What is its function? Could you please put its function in simpler words?

Thanks in advance.

like image 560
kartiksach Avatar asked Dec 15 '22 04:12

kartiksach


1 Answers

Have a look at the following figure for visual understanding of Key objects in an iOS app:

enter image description here

Role of AppDelegate:

The app delegate is the heart of your app code. It handles app initialization, state transitions, and many high-level app events. This object is also the only one guaranteed to be present in every app, so it is often used to set up the app’s initial data structures.

AppDelegate is used for the whole app, you can use it to manage the app life cycle, on the other hand, ViewController is used for a single view. you can use it to manage life cycle of a view. One app can have multiple views. but only one AppDelegate.

Role of NSManagedObjectContext:

enter image description here

The NSManagedObjectContext is a fundamental concept of Core Data.It is like transaction in a relational data. You can fetch objects, you can create objects , update and delete them, save them back to the persistent store, etc. Basically for all the core data operations, you will need to interact with NSManagedObjectContext.

like image 116
Vishal Sonawane Avatar answered Feb 23 '23 10:02

Vishal Sonawane