Book Image

Objective-C Memory Management Essentials

Book Image

Objective-C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Getting into the code


Let's get started with our code examples:

  1. For our code, we will first declare some Core Data objects in our AppDelegate class inside our AppDelegate.h file such as:

    @property (readonly, strong, nonatomic) NSManagedObjectContext
    *managedObjectContext;
    @property (readonly, strong, nonatomic) NSManagedObjectModel
    *managedObjectModel;
    @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator
    *persistentStoreCoordinator;

    These are declared here so that we can access them easily from any screen.

  2. Next, we will declare the code for each of the objects in AppDelegate.m such as the following lines of code that will create an instance of NSManagedObjectContext and return an existing instance if the instance already exists. This is important as you want only one instance of the context to be present to avoid conflicting access to the context:

    - (NSManagedObjectContext *)managedObjectContext
    {
        if (_managedObjectContext != nil) {
            return _managedObjectContext;
     ...