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

Deleting data from the persistent store


We will now move on to delete a record from the persistent store. In our table view, we will load the customers using an instance of NSFetchRequest, as shown:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    //Get the context first
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    
    //load data from Customer entity
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Customer"];
    self.customers = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    
    [tblView reloadData];
}

Here, we will declare customers as a mutable array to store our records from the Customer entity:

@property (strong) NSMutableArray *customers;

To delete a record, we just need to get our Customer record, which is an instance of NSManagedObject from the customers array, then use an instance of managedObjectContext to call the deleteObject method on...