0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · 课时

Core Data 基础

了解 Core Data 的基础知识,这是 Apple 用于管理对象图和持久化存储的框架。

Core Data 基础 是 CoddyKit 上的免费 Objective-C iOS Development for Legacy & Enterprise Apps 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Objective-C iOS Development for Legacy & Enterprise Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Welcome to Core Data

Hello! Today we're diving into Core Data, Apple's powerful framework for managing and persisting an application's object graph.

It helps you store your app's data on disk and retrieve it efficiently.

Core Data is Not a Database

It's crucial to understand: Core Data itself is not a database. Instead, it's an object graph management framework.

  • It helps you manage your data objects in memory.
  • It provides tools to save these objects to a persistent store (which *can* be a database like SQLite).
  • Think of it as an abstraction layer over the actual storage.

The Core Data Stack

Core Data relies on a 'stack' of objects that work together. Understanding these components is key:

  • Managed Object Model: Your app's data schema.
  • Persistent Store Coordinator: Connects the model to the store.
  • Persistent Store: The actual storage (e.g., SQLite file).
  • Managed Object Context: Your scratchpad for data interaction.

Managed Object Model (NSManagedObjectModel)

The Managed Object Model (NSManagedObjectModel) defines your application's data structure.

  • It's where you specify entities (like tables in a database).
  • Each entity has attributes (properties, e.g., name, age).
  • You can also define relationships between entities.

You typically create this visually in Xcode using the data model editor (a .xcdatamodeld file).

Persistent Store Coordinator (NSPersistentStoreCoordinator)

The Persistent Store Coordinator (NSPersistentStoreCoordinator) acts as a bridge.

  • It takes your NSManagedObjectModel.
  • It connects it to one or more persistent stores.
  • Common store types include SQLite, XML, or binary files. SQLite is the most common for iOS apps.

Persistent Store (NSPersistentStore)

The Persistent Store (NSPersistentStore) is where your data actually lives on disk.

When you choose an SQLite store, Core Data creates and manages an SQLite database file. You don't interact with SQL directly; Core Data handles it for you.

Managed Object Context (NSManagedObjectContext)

The Managed Object Context (NSManagedObjectContext) is your primary interface for interacting with Core Data.

  • Think of it as a 'scratchpad' where you create, retrieve, update, and delete objects.
  • Changes made here are temporary until you 'save' the context.
  • It tracks all changes and reports them to the Persistent Store Coordinator when saved.

Working with NSManagedObject

Data records in Core Data are represented by instances of NSManagedObject (or its subclasses).

They are similar to regular Objective-C objects, but their properties are managed by Core Data.

Here's how you'd typically create and set properties on such an object (assuming a context is ready):

#import <Foundation/Foundation.h>

// In a real Core Data app, 'Person' would be
// an NSManagedObject subclass generated from
// your data model.

@interface Person : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end

@implementation Person
@end

int main(int argc, const char * argv[]) {
  @autoreleasepool {
    // Normally, this object would be created
    // via NSManagedObjectContext's insertNewObjectForEntityForName method.
    // For this simple runnable example, we use a plain NSObject.
    Person *newPerson = [[Person alloc] init];
    newPerson.name = @"Alice";
    newPerson.age = 30;

    NSLog(@"Created person: %@, age: %ld", newPerson.name, (long)newPerson.age);
    // In a real Core Data app, you'd then save the context
    // to persist this NSManagedObject instance.
  }
  return 0;
}

Saving Your Changes

After you create, modify, or delete NSManagedObject instances within a NSManagedObjectContext, these changes are only in memory.

To persist them to the Persistent Store, you must tell the context to save:

NSError *error = nil;
[context save:&error];
if (error) {
NSLog(@"Error saving context: %@", error);
}

It's crucial to always check for errors when saving!

Quick Check: Core Data Roles

Which Core Data component is responsible for defining your application's data structure, including entities and their attributes?

Core Data Fundamentals Recap

We've covered the basics of Core Data, Apple's object graph management framework. Remember:

  • It's not a database, but uses them for storage.
  • The Core Data Stack includes Model, Coordinator, Store, and Context.
  • Managed Object Model defines your data.
  • Persistent Store Coordinator connects model to store.
  • Persistent Store is the physical data location.
  • Managed Object Context is your interaction point.
  • You interact with NSManagedObject instances and must save the context to persist changes.

Next, we'll explore fetching data and more complex interactions!

常见问题解答

「Core Data 基础」课时是免费的吗?

是的 — 「Core Data 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Objective-C iOS Development for Legacy & Enterprise Apps 课程的其余内容,请升级到 CoddyKit PRO。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

「Core Data 基础」这节课中我会学到什么?

了解 Core Data 的基础知识,这是 Apple 用于管理对象图和持久化存储的框架。 你通过在浏览器中直接运行的动手代码来练习 Objective-C iOS Development for Legacy & Enterprise Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Objective-C iOS Development for Legacy & Enterprise Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Objective-C iOS Development for Legacy & Enterprise Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「Core Data 基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Objective-C iOS Development for Legacy & Enterprise Apps 课中编写并运行代码吗?

能。每节 Objective-C iOS Development for Legacy & Enterprise Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 属性列表与归档
  2. Core Data 基础
  3. 使用 FMDB 集成 SQLite
  4. 使用 NSUserDefaults 存储偏好设置
← 返回 Objective-C iOS Development for Legacy & Enterprise Apps