Realm vs Core Data
iOS 与移动开发中本地数据库领域的两大强者:反应式跨平台的 Realm,对决 Apple 深度集成的对象图框架 Core Data。
SQLite 的强大 + Dart 的类型安全——对懂 SQL 的人来说是自然的选择
NoSQL 的速度与简洁——但首先要选对包
明确的答案是:如果你的数据模型是关系型的,且团队具备 SQL 知识,Drift 是更稳妥的选择——2.35.0 版本发布于 2026-09-09,最近一次提交在 2026-09-24,月下载量超过 130 万。在 Isar 这边,唯一关键的一点是 pubspec.yaml 中要写的是 `isar_community`;原始的 `isar` 包自 2023 年起就已经停滞。该分支(fork)依然活跃,对于类文档型数据(缓存、模式灵活的笔记)仍然是合理的选择。
| 分类 | Drift | Isar |
|---|---|---|
| 性能 | 8/10 | 8/10 |
| 学习难易度 | 6/10 | 8/10 |
| 生态系统 | 9/10 | 5/10 |
| 社区 | 8/10 | 5/10 |
| 就业市场 | 6/10 | 5/10 |
| 面向未来 | 9/10 | 6/10 |
// Drift - 表定义 + 响应式查询 (drift.simonbinder.eu/setup/)
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
part 'database.g.dart';
class Todos extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 1, max: 200)();
BoolColumn get isDone => boolean().withDefault(const Constant(false))();
}
@DriftDatabase(tables: [Todos])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
// 响应式数据流:表发生变化时自动推送
Stream<List<Todo>> watchPendingTodos() {
return (select(todos)..where((t) => t.isDone.equals(false))).watch();
}
Future<int> addTodo(String title) {
return into(todos).insert(TodosCompanion.insert(title: title));
}
}
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return NativeDatabase.createInBackground(file);
});
}// isar_community - 集合定义 + 查询 (pub.dev/packages/isar_community readme 示例模式)
import 'package:isar_community/isar.dart';
import 'package:path_provider/path_provider.dart';
part 'todo.g.dart';
@collection
class Todo {
Id id = Isar.autoIncrement;
@Index()
late String title;
bool isDone = false;
}
Future<Isar> openDb() async {
final dir = await getApplicationDocumentsDirectory();
return Isar.open(
[TodoSchema],
directory: dir.path,
);
}
Future<void> addTodo(Isar isar, String title) async {
final todo = Todo()..title = title;
await isar.writeTxn(() async {
await isar.todos.put(todo);
});
}
// 响应式查询:集合发生变化时自动推送
Stream<List<Todo>> watchPendingTodos(Isar isar) {
return isar.todos.filter().isDoneEqualTo(false).watch(fireImmediately: true);
}明确的答案是:如果你的数据模型是关系型的,且团队具备 SQL 知识,Drift 是更稳妥的选择——2.35.0 版本发布于 2026-09-09,最近一次提交在 2026-09-24,月下载量超过 130 万。在 Isar 这边,唯一关键的一点是 pubspec.yaml 中要写的是 `isar_community`;原始的 `isar` 包自 2023 年起就已经停滞。该分支(fork)依然活跃,对于类文档型数据(缓存、模式灵活的笔记)仍然是合理的选择。
获取免费咨询在新项目中一定要使用 isar_community。原始的 isar 包在 pub.dev 上自 2023-04-25 起、在 GitHub 上自 2025-06-14 起就再未更新——实际上已被放弃。isar_community 是社区接手维护的活跃分支,如今提到“Isar”指的应该就是这个包。