用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/a5c-ai/babysitter --skill ios-persistence-core-data-realm命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
基于 SOC 职业分类
| name | iOS Persistence (Core Data/Realm) |
| description | Specialized skill for iOS local data persistence solutions |
| version | 1.0.0 |
| category | iOS Data Storage |
| slug | ios-persistence |
| status | active |
| graph | {"domains":["domain:mobile"],"specializations":["specialization:mobile-development"],"skillAreas":["skill-area:ios-native","skill-area:mobile-local-databases"],"roles":["role:mobile-engineer"],"workflows":["workflow:feature-development","workflow:release-management"],"topics":["topic:accessibility"]} |
This skill provides specialized capabilities for iOS local data persistence solutions including Core Data and Realm. It enables designing data models, implementing migrations, configuring iCloud sync, and optimizing database performance.
bash - Execute xcodebuild and swift commandsread - Analyze Core Data models and Realm schemaswrite - Generate model classes and configurationsedit - Update existing persistence codeglob - Search for model files and configurationsgrep - Search for patterns in persistence codeModel Design
CRUD Operations
Migrations
CloudKit Integration
Performance Optimization
Schema Definition
Queries and Filtering
Migrations
Sync Configuration
This skill integrates with the following processes:
ios-core-data-implementation.js - Core Data setup and usageoffline-first-architecture.js - Offline data strategiesmobile-security-implementation.js - Secure data storage// Persistence/PersistenceController.swift
import CoreData
import CloudKit
final class PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentCloudKitContainer
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "MyApp")
if inMemory {
container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
}
// Configure CloudKit
guard let description = container.persistentStoreDescriptions.first else {
fatalError("Failed to retrieve persistent store description")
}
description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(
containerIdentifier: "iCloud.com.example.myapp"
)
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
container.loadPersistentStores { description, error in
error error {
()
}
}
container.viewContext.automaticallyMergesChangesFromParent
container.viewContext.mergePolicy
}
preview: {
controller (inMemory: )
controller
}()
}
// Persistence/RealmManager.swift
import RealmSwift
final class RealmManager {
static let shared = RealmManager()
private init() {
configureRealm()
}
private func configureRealm() {
let config = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
// Migration logic
}
}
)
Realm.Configuration.defaultConfiguration = config
}
var realm: Realm {
try! Realm()
}
}
// Models/Item+CoreDataClass.swift
import Foundation
import CoreData
@objc(Item)
public class Item: NSManagedObject {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Item> {
return NSFetchRequest<Item>(entityName: "Item")
}
@NSManaged public var id: UUID
@NSManaged public var title: String
@NSManaged public var createdAt: Date
@NSManaged public var isCompleted: Bool
@NSManaged public var category: Category?
}
extension Item {
static func create(
in context: NSManagedObjectContext,
title: String,
category: Category? = nil
) -> {
item (context: context)
item.id ()
item.title title
item.createdAt ()
item.isCompleted
item.category category
item
}
( : ) -> [] {
request fetchRequest()
request.sortDescriptors [(keyPath: \.createdAt, ascending: )]
( context.fetch(request)) []
}
( : ) -> [] {
request fetchRequest()
request.predicate (format: )
request.sortDescriptors [(keyPath: \.createdAt, ascending: )]
( context.fetch(request)) []
}
}
// Data/Repository/ItemRepository.swift
import Foundation
import CoreData
import Combine
protocol ItemRepositoryProtocol {
func fetchItems() -> AnyPublisher<[Item], Error>
func addItem(title: String) -> AnyPublisher<Item, Error>
func updateItem(_ item: Item) -> AnyPublisher<Void, Error>
func deleteItem(_ item: Item) -> AnyPublisher<Void, Error>
}
final class ItemRepository: ItemRepositoryProtocol {
private let container: NSPersistentContainer
private let backgroundContext: NSManagedObjectContext
init(container: NSPersistentContainer = PersistenceController.shared.container) {
self.container = container
self.backgroundContext container.newBackgroundContext()
.backgroundContext.mergePolicy
}
() -> <[], > {
{ [ ] promise
{ }
.backgroundContext.perform {
{
items .fetchAll(in: .backgroundContext)
promise(.success(items))
} {
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
}
(: ) -> <, > {
{ [ ] promise
{ }
.backgroundContext.perform {
item .create(in: .backgroundContext, title: title)
{
.backgroundContext.save()
promise(.success(item))
} {
.backgroundContext.rollback()
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
}
( : ) -> <, > {
{ [ ] promise
{ }
.backgroundContext.perform {
{
.backgroundContext.save()
promise(.success(()))
} {
.backgroundContext.rollback()
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
}
( : ) -> <, > {
{ [ ] promise
{ }
.backgroundContext.perform {
.backgroundContext.delete(item)
{
.backgroundContext.save()
promise(.success(()))
} {
.backgroundContext.rollback()
promise(.failure(error))
}
}
}
.eraseToAnyPublisher()
}
}
// Models/TaskObject.swift
import RealmSwift
class TaskObject: Object, Identifiable {
@Persisted(primaryKey: true) var id: ObjectId
@Persisted var title: String = ""
@Persisted var dueDate: Date?
@Persisted var isCompleted: Bool = false
@Persisted var priority: Int = 0
@Persisted var tags: List<TagObject>
@Persisted(originProperty: "tasks") var project: LinkingObjects<ProjectObject>
convenience init(title: String, dueDate: Date? = nil, priority: Int = 0) {
self.init()
self.title = title
self.dueDate = dueDate
.priority priority
}
}
: , {
(primaryKey: ) id:
(indexed: ) name:
color:
}
: , {
(primaryKey: ) id:
name:
tasks: <>
}
// Data/Repository/TaskRealmRepository.swift
import Foundation
import RealmSwift
import Combine
protocol TaskRepositoryProtocol {
func fetchTasks() -> AnyPublisher<[TaskObject], Error>
func addTask(_ task: TaskObject) -> AnyPublisher<Void, Error>
func updateTask(_ task: TaskObject, with updates: (TaskObject) -> Void) -> AnyPublisher<Void, Error>
func deleteTask(_ task: TaskObject) -> AnyPublisher<Void, Error>
}
final class TaskRealmRepository: TaskRepositoryProtocol {
private let realm: Realm
init(realm: Realm = RealmManager.shared.realm) {
self.realm = realm
}
() -> <[], > {
((realm.objects(.).sorted(byKeyPath: )))
.setFailureType(to: .)
.eraseToAnyPublisher()
}
( : ) -> <, > {
{ [ ] promise
{ }
{
.realm.write {
.realm.add(task)
}
promise(.success(()))
} {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
( : , : () -> ) -> <, > {
{ [ ] promise
{ }
{
.realm.write {
updates(task)
}
promise(.success(()))
} {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
( : ) -> <, > {
{ [ ] promise
{ }
{
.realm.write {
.realm.delete(task)
}
promise(.success(()))
} {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
}
swift-swiftui - iOS app developmentmobile-security - Secure data storageoffline-storage - Cross-platform offline patterns