Skip to content

Commit

Permalink
Restore support for Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
lickel committed Sep 26, 2024
1 parent 1baa95f commit 62491e8
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ playground.xcworkspace
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
Package.resolved
.build/
.swiftpm/

# CocoaPods
#
Expand Down
2 changes: 1 addition & 1 deletion FetchRequests.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Pod::Spec.new do |s|
s.macos.deployment_target = macos_deployment_target
s.visionos.deployment_target = visionos_deployment_target

s.swift_version = '5.0'
s.swift_versions = ['5.0', '6.0']

s.source_files = [
'FetchRequests/simplediff-swift/simplediff.swift',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import Foundation

// MARK: - Internal Structures

struct AssociatedValueKey<FetchedObject: FetchableObject>: Hashable, Sendable {
struct AssociatedValueKey<FetchedObject: FetchableObject>: Hashable {
var id: FetchedObject.ID
var keyPath: PartialKeyPath<FetchedObject> & Sendable
var keyPath: FetchRequestAssociation<FetchedObject>.AssociationKeyPath
}

#if compiler(>=6)
extension AssociatedValueKey: Sendable {}
#else
extension AssociatedValueKey: @unchecked Sendable {}
#endif

class FetchableAssociatedValueReference<Entity: FetchableObject>: AssociatedValueReference, @unchecked Sendable {
private var observations: [Entity: [InvalidatableToken]] = [:]

Expand Down
38 changes: 24 additions & 14 deletions FetchRequests/Sources/Associations/FetchRequestAssociation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ public class FetchRequestAssociation<FetchedObject: FetchableObject> {
/// Start observing a source object
public typealias TokenGenerator<Source, Token: ObservableToken> = (Source) -> Token?

#if compiler(>=6)
internal typealias AssociationKeyPath = PartialKeyPath<FetchedObject> & Sendable
#else
internal typealias AssociationKeyPath = PartialKeyPath<FetchedObject>
#endif
internal typealias ReferenceGenerator = (FetchedObject) -> AssociatedValueReference

#if compiler(>=6)
public typealias EntityKeyPath<T> = KeyPath<FetchedObject, T> & Sendable
#else
public typealias EntityKeyPath<T> = KeyPath<FetchedObject, T>
#endif

internal let keyPath: AssociationKeyPath
internal let request: AssocationRequestByParent<Any>
internal let referenceGenerator: ReferenceGenerator
Expand Down Expand Up @@ -87,7 +97,7 @@ public class FetchRequestAssociation<FetchedObject: FetchableObject> {
public extension FetchRequestAssociation {
/// Association by non-optional entity ID
convenience init<AssociatedEntity, AssociatedEntityID: Equatable>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID>,
request: @escaping AssocationRequestByParent<AssociatedEntity>
) {
self.init(
Expand Down Expand Up @@ -115,7 +125,7 @@ public extension FetchRequestAssociation {

/// Association by optional entity ID
convenience init<AssociatedEntity, AssociatedEntityID: Equatable>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID?> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID?>,
request: @escaping AssocationRequestByParent<AssociatedEntity>
) {
self.init(
Expand Down Expand Up @@ -152,7 +162,7 @@ public extension FetchRequestAssociation {
AssociatedEntityID: Equatable,
Token: ObservableToken<RawAssociatedEntity>
>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID>,
request: @escaping AssocationRequestByParent<AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<FetchedObject, Token>,
creationObserved: @escaping CreationObserved<AssociatedEntity, RawAssociatedEntity>
Expand Down Expand Up @@ -219,7 +229,7 @@ public extension FetchRequestAssociation {
AssociatedEntityID: Equatable,
Token: ObservableToken<RawAssociatedEntity>
>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID?> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID?>,
request: @escaping AssocationRequestByParent<AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<FetchedObject, Token>,
creationObserved: @escaping CreationObserved<AssociatedEntity, RawAssociatedEntity>
Expand Down Expand Up @@ -285,7 +295,7 @@ public extension FetchRequestAssociation {
AssociatedEntityID: Equatable,
Token: ObservableToken<AssociatedEntity>
>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID>,
request: @escaping AssocationRequestByParent<AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<FetchedObject, Token>,
preferExistingValueOnCreate: Bool
Expand All @@ -309,7 +319,7 @@ public extension FetchRequestAssociation {
AssociatedEntityID: Equatable,
Token: ObservableToken<AssociatedEntity>
>(
keyPath: KeyPath<FetchedObject, AssociatedEntityID?> & Sendable,
keyPath: EntityKeyPath<AssociatedEntityID?>,
request: @escaping AssocationRequestByParent<AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<FetchedObject, Token>,
preferExistingValueOnCreate: Bool
Expand Down Expand Up @@ -337,7 +347,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: AssociatedEntity.Type,
keyPath: KeyPath<FetchedObject, AssociatedEntity.ID> & Sendable,
keyPath: EntityKeyPath<AssociatedEntity.ID>,
request: @escaping AssocationRequestByID<AssociatedEntity.ID, AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<AssociatedEntity.ID, Token>,
preferExistingValueOnCreate: Bool
Expand Down Expand Up @@ -427,7 +437,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: AssociatedEntity.Type,
keyPath: KeyPath<FetchedObject, AssociatedEntity.ID?> & Sendable,
keyPath: EntityKeyPath<AssociatedEntity.ID?>,
request: @escaping AssocationRequestByID<AssociatedEntity.ID, AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<AssociatedEntity.ID, Token>,
preferExistingValueOnCreate: Bool
Expand Down Expand Up @@ -525,7 +535,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: [AssociatedEntity].Type,
keyPath: KeyPath<FetchedObject, [AssociatedEntity.ID]> & Sendable,
keyPath: EntityKeyPath<[AssociatedEntity.ID]>,
request: @escaping AssocationRequestByID<AssociatedEntity.ID, AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<[AssociatedEntity.ID], Token>,
creationObserved: @escaping CreationObserved<[AssociatedEntity], AssociatedEntity.RawData>
Expand All @@ -547,7 +557,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: [AssociatedEntity].Type,
keyPath: KeyPath<FetchedObject, [Reference]> & Sendable,
keyPath: EntityKeyPath<[Reference]>,
request: @escaping AssocationRequestByID<Reference, AssociatedEntity>,
referenceAccessor: @escaping @Sendable (AssociatedEntity) -> Reference,
creationTokenGenerator: @escaping TokenGenerator<[Reference], Token>,
Expand Down Expand Up @@ -653,7 +663,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: [AssociatedEntity].Type,
keyPath: KeyPath<FetchedObject, [AssociatedEntity.ID]?> & Sendable,
keyPath: EntityKeyPath<[AssociatedEntity.ID]?>,
request: @escaping AssocationRequestByID<AssociatedEntity.ID, AssociatedEntity>,
creationTokenGenerator: @escaping TokenGenerator<[AssociatedEntity.ID], Token>,
creationObserved: @escaping CreationObserved<[AssociatedEntity], AssociatedEntity.RawData>
Expand All @@ -675,7 +685,7 @@ public extension FetchRequestAssociation {
Token: ObservableToken<AssociatedEntity.RawData>
>(
for associatedType: [AssociatedEntity].Type,
keyPath: KeyPath<FetchedObject, [Reference]?> & Sendable,
keyPath: EntityKeyPath<[Reference]?>,
request: @escaping AssocationRequestByID<Reference, AssociatedEntity>,
referenceAccessor: @escaping @Sendable (AssociatedEntity) -> Reference,
creationTokenGenerator: @escaping TokenGenerator<[Reference], Token>,
Expand Down Expand Up @@ -784,7 +794,7 @@ public extension FetchRequestAssociation {
EntityID: FetchableEntityID,
Token: ObservableToken<EntityID.FetchableEntity.RawData>
>(
keyPath: KeyPath<FetchedObject, EntityID> & Sendable,
keyPath: EntityKeyPath<EntityID>,
creationTokenGenerator: @escaping TokenGenerator<EntityID, Token>,
preferExistingValueOnCreate: Bool
) {
Expand Down Expand Up @@ -856,7 +866,7 @@ public extension FetchRequestAssociation {
EntityID: FetchableEntityID,
Token: ObservableToken<EntityID.FetchableEntity.RawData>
>(
keyPath: KeyPath<FetchedObject, EntityID?> & Sendable,
keyPath: EntityKeyPath<EntityID?>,
creationTokenGenerator: @escaping TokenGenerator<EntityID, Token>,
preferExistingValueOnCreate: Bool
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class CollapsibleSectionsFetchedResultsController<FetchedObject: Fetchabl
public typealias Section = CollapsibleResultsSection<FetchedObject>
public typealias SectionCollapseCheck = (_ section: BackingFetchController.Section) -> Bool
public typealias SectionCollapseConfigCheck = (_ section: BackingFetchController.Section) -> SectionCollapseConfig?
public typealias SectionNameKeyPath = KeyPath<FetchedObject, String> & Sendable
public typealias SectionNameKeyPath = FetchedResultsController<FetchedObject>.SectionNameKeyPath

private var changedSectionsDuringContentChange: Set<String> = []
private var deletedSectionsDuringContentChange: Set<String> = []
Expand Down Expand Up @@ -418,33 +418,73 @@ private class DelegateThunk<FetchedObject: FetchableObject> {

private weak var parent: (any Parent)?

#if compiler(<6)
private let willChange: @MainActor (_ controller: Controller) -> Void
private let didChange: @MainActor (_ controller: Controller) -> Void

private let changeObject: @MainActor (_ controller: Controller, _ object: FetchedObject, _ change: FetchedResultsChange<IndexPath>) -> Void
private let changeSection: @MainActor (_ controller: Controller, _ section: Section, _ change: FetchedResultsChange<Int>) -> Void
#endif

init(_ parent: some Parent) {
self.parent = parent

#if compiler(<6)
willChange = { [weak parent] controller in
parent?.controllerWillChangeContent(controller)
}
didChange = { [weak parent] controller in
parent?.controllerDidChangeContent(controller)
}

changeObject = { [weak parent] controller, object, change in
parent?.controller(controller, didChange: object, for: change)
}
changeSection = { [weak parent] controller, section, change in
parent?.controller(controller, didChange: section, for: change)
}
#endif
}
}

extension DelegateThunk: CollapsibleSectionsFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: Controller) {
#if compiler(>=6)
self.parent?.controllerWillChangeContent(controller)
#else
self.willChange(controller)
#endif
}

func controllerDidChangeContent(_ controller: Controller) {
#if compiler(>=6)
self.parent?.controllerDidChangeContent(controller)
#else
self.didChange(controller)
#endif
}

func controller(
_ controller: Controller,
didChange object: FetchedObject,
for change: FetchedResultsChange<IndexPath>
) {
#if compiler(>=6)
self.parent?.controller(controller, didChange: object, for: change)
#else
self.changeObject(controller, object, change)
#endif
}

func controller(
_ controller: Controller,
didChange section: Section,
for change: FetchedResultsChange<Int>
) {
#if compiler(>=6)
self.parent?.controller(controller, didChange: section, for: change)
#else
self.changeSection(controller, section, change)
#endif
}
}
Loading

0 comments on commit 62491e8

Please sign in to comment.