Core Data Fundamentals: Building a Note-Taking App with CRUD Operations

Core Data implementation for a personal note-taking application demonstrates basic data persistence patterns. This implementation includes create, read, update, delete operations along with sorting and pagination capabilities.

  1. Core Data architecture maps database components to object-oriented constructs:
  • NSManagedObjectModel represents the schema of all defined entities
  • Each database table corresponds to an Entity definition
  • Table records become NSManagedObject instances
  • Field values map to NSManagedObject properties
  1. Project configuration requires enabling Core Data in Xcode project settings. The framework automatically generates these key components in AppDelegate:
@property (readonly, strong, nonatomic) NSManagedObjectContext *context;
@property (readonly, strong, nonatomic) NSManagedObjectModel *model;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *coordinator;

Context manages object lifecycle, model defines data schema, and coordinator handles persistence coordination between memory and storage.

  1. Entity definitions are created using .xcdatamodeld files. After defining attributes, generate corresponding class files through Xcode's code generation wizard. For an Article entity:
// Generated class for Article entity
@interface Article : NSManagedObject
@property NSString *title;
@property NSData *content;
@property NSDate *createTime;
@end

  1. CRUD operations require accessing the managed object context:
...
@property (nonatomic) NSManagedObjectContext *context;
...
self.context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] persistentContainer].viewContext;
...

  1. Create operation implementation:
- (void)createNote {
    NSString *noteTitle = self.titleTextField.text;
    NSString *noteContent = self.contentTextView.text;

    Article *newNote = [NSEntityDescription insertNewObjectForEntityForName:@"Article" inManagedObjectContext:self.context];
    newNote.title = noteTitle;
    newNote.content = [noteContent dataUsingEncoding:NSUTF8StringEncoding];
    newNote.createTime = [NSDate date];

    NSError *error = nil;
    if (![self.context save:&error]) {
        NSLog(@"Creation failed: %@", error.localizedDescription);
    }
}

  1. Delete operation implementation:
- (void)deleteNote:(Article *)note {
    [self.context delete:note];
    NSError *error = nil;
    if (![self.context save:&error]) {
        NSLog(@"Deletion failed: %@", error.localizedDescription);
    }
}

  1. Update operation implementation:
- (void)updateNote {
    Article *existingNote = [self.context objectWithID:self.noteID];
    existingNote.title = self.titleTextField.text;
    existingNote.content = [self.contentTextView.text dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error = nil;
    if (![self.context save:&error]) {
        NSLog(@"Update failed: %@", error.localizedDescription);
    }
}

  1. Query operation with sorting and pagination:
- (NSArray<Note *> *)fetchNotes {
    NSFetchRequest<Note *> *request = [NSFetchRequest fetchRequestWithEntityName:@"Note"];
    [request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"createTime" ascending:NO]]];
    
    NSInteger itemsPerPage = 10;
    NSInteger currentPage = 2;
    [request setFetchLimit:itemsPerPage];
    [request setFetchOffset:currentPage * itemsPerPage];
    
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"title CONTAINS[c] %@", self.searchQuery];
    [request setPredicate:filter];
    
    NSError *error = nil;
    return [self.context executeFetchRequest:request error:&error];
}

  1. Implementation demonstrates standard Core Data patterns for basic data management operations.

Tags: Core Data iOS Development Swift NSSortDescriptor

Posted on Tue, 23 Jun 2026 17:04:00 +0000 by GregL83