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.
- 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
- 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.
- 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
- CRUD operations require accessing the managed object context:
...
@property (nonatomic) NSManagedObjectContext *context;
...
self.context = [(AppDelegate*)[[UIApplication sharedApplication] delegate] persistentContainer].viewContext;
...
- 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);
}
}
- Delete operation implementation:
- (void)deleteNote:(Article *)note {
[self.context delete:note];
NSError *error = nil;
if (![self.context save:&error]) {
NSLog(@"Deletion failed: %@", error.localizedDescription);
}
}
- 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);
}
}
- 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];
}
- Implementation demonstrates standard Core Data patterns for basic data management operations.