One Logo Pool Ball
SwiftData · IOS API REFERENCE

ModelContext

class · iOS 17.0+ · SDK 27.1

class ModelContext

An object that enables you to fetch, insert, and delete models, and save any changes to disk.

Members (23)

get · iOS 17.0+

hasChanges

TypeScript
get hasChanges(): boolean
Swift declaration
var hasChanges: Bool { get }
method · iOS 17.0+

transaction

TypeScript
transaction(block: (() => void)): void
Swift declaration
func transaction(block: () throws -> Void) throws
get · iOS 17.0+

undoManager

TypeScript
get undoManager(): (_Foundation_UndoManager) | null
Swift declaration
var undoManager: UndoManager? { get set }
set · iOS 17.0+

undoManager

TypeScript
set undoManager(value: (_Foundation_UndoManager) | null)
Swift declaration
var undoManager: UndoManager? { get set }
get · iOS 18.0+

editingState

TypeScript
get editingState(): EditingState
Swift declaration
var editingState: EditingState
set · iOS 18.0+

editingState

TypeScript
set editingState(value: EditingState)
Swift declaration
var editingState: EditingState
method · iOS 18.0+

fetchHistory

TypeScript
fetchHistory(descriptor: HistoryDescriptor<DefaultHistoryTransaction>): Array<DefaultHistoryTransaction>
Swift declaration
func fetchHistory<T>(_ descriptor: HistoryDescriptor<T>) throws -> [T] where T : HistoryTransaction
method · iOS 18.0+

deleteHistory

TypeScript
deleteHistory(descriptor: HistoryDescriptor<DefaultHistoryTransaction>): void
Swift declaration
func deleteHistory<T>(_ descriptor: HistoryDescriptor<T>) throws where T : HistoryTransaction
get · iOS 17.0+

autosaveEnabled

TypeScript
get autosaveEnabled(): boolean
Swift declaration
var autosaveEnabled: Bool { get set }
set · iOS 17.0+

autosaveEnabled

TypeScript
set autosaveEnabled(value: boolean)
Swift declaration
var autosaveEnabled: Bool { get set }
get · iOS 17.0+

changedModelsArray

TypeScript
get changedModelsArray(): Array<SDKHandle>
Swift declaration
var changedModelsArray: [any PersistentModel] { get }
get · iOS 17.0+

deletedModelsArray

TypeScript
get deletedModelsArray(): Array<SDKHandle>
Swift declaration
var deletedModelsArray: [any PersistentModel] { get }
get · iOS 17.0+

insertedModelsArray

TypeScript
get insertedModelsArray(): Array<SDKHandle>
Swift declaration
var insertedModelsArray: [any PersistentModel] { get }
method · iOS 17.0+

processPendingChanges

TypeScript
processPendingChanges(): void
Swift declaration
func processPendingChanges()
method · iOS 17.0+

save

TypeScript
save(): void
Swift declaration
func save() throws
method · iOS 17.0+

model

TypeScript
model(persistentModelID: PersistentIdentifier): SDKHandle
Swift declaration
func model(for persistentModelID: PersistentIdentifier) -> any PersistentModel
get · iOS 18.0+

author

TypeScript
get author(): (string) | null
Swift declaration
var author: String? { get set }
set · iOS 18.0+

author

TypeScript
set author(value: (string) | null)
Swift declaration
var author: String? { get set }
get · iOS 17.0+

didSave

TypeScript
static get didSave(): _Foundation_NSNotification_Name
Swift declaration
static let didSave: Notification.Name
method · iOS 17.0+

rollback

TypeScript
rollback(): void
Swift declaration
func rollback()
get · iOS 17.0+

willSave

TypeScript
static get willSave(): _Foundation_NSNotification_Name
Swift declaration
static let willSave: Notification.Name
get · iOS 17.0+

container

TypeScript
get container(): ModelContainer
Swift declaration
var container: ModelContainer { get }
init · iOS 17.0+

init

TypeScript
static init(container: ModelContainer): ModelContext
Swift declaration
init(_ container: ModelContainer)

Skipped declarations

10 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

ModelContext.registeredModel(for:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.insert(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.delete(model:where:includeSubclasses:)

Passing a Swift type itself is not supported.

ModelContext.delete(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.enumerate(_:batchSize:allowEscapingMutations:block:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.fetch(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.fetchCount(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.fetch(_:batchSize:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.fetchIdentifiers(_:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

ModelContext.fetchIdentifiers(_:batchSize:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

All SwiftData types →