Skip to content

Commit

Permalink
one more thing
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 16, 2024
1 parent e67db79 commit 5a104c5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Sources/DataThespian/SwiftData/ModelContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,27 @@
extension ModelContext {
public func get<T>(_ model: Model<T>) throws -> T?
where T: PersistentModel {
try self.existingModel(for: model.persistentIdentifier)
try self.persistentModel(withID: model.persistentIdentifier)

Check warning on line 37 in Sources/DataThespian/SwiftData/ModelContext.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DataThespian/SwiftData/ModelContext.swift#L36-L37

Added lines #L36 - L37 were not covered by tests
}

private func persistentModel<T>(withID objectID: PersistentIdentifier) throws -> T?
where T: PersistentModel {
if let registered: T = registeredModel(for: objectID) {
return registered

Check warning on line 43 in Sources/DataThespian/SwiftData/ModelContext.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DataThespian/SwiftData/ModelContext.swift#L41-L43

Added lines #L41 - L43 were not covered by tests
}
if let notRegistered: T = model(for: objectID) as? T {
return notRegistered

Check warning on line 46 in Sources/DataThespian/SwiftData/ModelContext.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DataThespian/SwiftData/ModelContext.swift#L45-L46

Added lines #L45 - L46 were not covered by tests
}

let fetchDescriptor = FetchDescriptor<T>(
predicate: #Predicate { $0.persistentModelID == objectID },
fetchLimit: 1
)

Check warning on line 52 in Sources/DataThespian/SwiftData/ModelContext.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DataThespian/SwiftData/ModelContext.swift#L49-L52

Added lines #L49 - L52 were not covered by tests

return try fetch(fetchDescriptor).first

Check warning on line 54 in Sources/DataThespian/SwiftData/ModelContext.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DataThespian/SwiftData/ModelContext.swift#L54

Added line #L54 was not covered by tests
}

@available(*, deprecated)
internal func existingModel<T>(for objectID: PersistentIdentifier) throws -> T?
where T: PersistentModel {
if let registered: T = registeredModel(for: objectID) {
Expand Down

0 comments on commit 5a104c5

Please sign in to comment.