Skip to content

Commit

Permalink
Removing some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
lickel committed Aug 30, 2024
1 parent 7580df5 commit 6cd2508
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"originHash" : "51f90653b2c9f9f7064c0d52159b40bf7d222e5f314be23e62fe28520fec03db",
"pins" : [
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
"revision" : "9bf03ff58ce34478e66aaee630e491823326fd06",
"version" : "1.1.3"
}
}
],
"version" : 2
"version" : 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

// MARK: - Internal Structures

struct AssociatedValueKey<FetchedObject: FetchableObject>: Hashable {
struct AssociatedValueKey<FetchedObject: FetchableObject>: Hashable, @unchecked Sendable {
var id: FetchedObject.ID
var keyPath: PartialKeyPath<FetchedObject>
}
Expand Down Expand Up @@ -70,7 +70,7 @@ class FetchableAssociatedValueReference<Entity: FetchableObject>: AssociatedValu
}
}

class AssociatedValueReference: NSObject {
class AssociatedValueReference: NSObject, @unchecked Sendable {
typealias CreationObserved = @MainActor (_ value: Any?, _ entity: Any) -> AssociationReplacement<Any>
typealias ChangeHandler = @MainActor (_ invalidate: Bool) -> Void

Expand Down Expand Up @@ -118,7 +118,7 @@ extension AssociatedValueReference {
startObservingValue()

creationObserver?.observeIfNeeded { [weak self] entity in
performOnMainThread {
performOnMainThread { [weak self] in
self?.observedCreationEvent(with: entity)
}
}
Expand Down
6 changes: 3 additions & 3 deletions FetchRequests/Sources/Associations/ObservableToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public protocol InvalidatableToken: AnyObject {
public protocol ObservableToken<Parameter>: InvalidatableToken {
associatedtype Parameter

func observe(handler: @escaping (Parameter) -> Void)
func observe(handler: @escaping @Sendable (Parameter) -> Void)
}

public class ObservableNotificationCenterToken: ObservableToken {
Expand All @@ -40,7 +40,7 @@ public class ObservableNotificationCenterToken: ObservableToken {
self.notificationCenter = notificationCenter
}

public func observe(handler: @escaping (Notification) -> Void) {
public func observe(handler: @escaping @Sendable (Notification) -> Void) {
centerToken = notificationCenter.addObserver(
forName: name,
object: nil,
Expand Down Expand Up @@ -148,7 +148,7 @@ internal class LegacyKeyValueObserving<Object: NSObject, Value: Any>: NSObject,
}

internal class FetchRequestObservableToken<Parameter>: ObservableToken {
typealias Handler = (Parameter) -> Void
typealias Handler = @Sendable (Parameter) -> Void

private let _observe: (_ handler: @escaping Handler) -> Void
private let _invalidate: () -> Void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,53 +418,33 @@ private class DelegateThunk<FetchedObject: FetchableObject> {

private weak var parent: (any Parent)?

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

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

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)
}
}
}

extension DelegateThunk: CollapsibleSectionsFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: Controller) {
self.willChange(controller)
self.parent?.controllerWillChangeContent(controller)
}

func controllerDidChangeContent(_ controller: Controller) {
self.didChange(controller)
self.parent?.controllerDidChangeContent(controller)
}

func controller(
_ controller: Controller,
didChange object: FetchedObject,
for change: FetchedResultsChange<IndexPath>
) {
self.changeObject(controller, object, change)
self.parent?.controller(controller, didChange: object, for: change)
}

func controller(
_ controller: Controller,
didChange section: Section,
for change: FetchedResultsChange<Int>
) {
self.changeSection(controller, section, change)
self.parent?.controller(controller, didChange: section, for: change)
}
}
53 changes: 16 additions & 37 deletions FetchRequests/Sources/Controller/FetchedResultsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,61 +85,41 @@ public extension FetchedResultsControllerDelegate {

// MARK: - DelegateThunk

private class DelegateThunk<FetchedObject: FetchableObject> {
private class DelegateThunk<FetchedObject: FetchableObject>: @unchecked Sendable {
typealias Parent = FetchedResultsControllerDelegate<FetchedObject>
typealias Controller = FetchedResultsController<FetchedObject>
typealias Section = FetchedResultsSection<FetchedObject>

private weak var parent: (any Parent)?

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

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

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)
}
}
}

extension DelegateThunk: FetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: Controller) {
self.willChange(controller)
self.parent?.controllerWillChangeContent(controller)
}

func controllerDidChangeContent(_ controller: Controller) {
self.didChange(controller)
self.parent?.controllerDidChangeContent(controller)
}

func controller(
_ controller: Controller,
didChange object: FetchedObject,
for change: FetchedResultsChange<IndexPath>
) {
self.changeObject(controller, object, change)
self.parent?.controller(controller, didChange: object, for: change)
}

func controller(
_ controller: Controller,
didChange section: Section,
for change: FetchedResultsChange<Int>
) {
self.changeSection(controller, section, change)
self.parent?.controller(controller, didChange: section, for: change)
}
}

Expand Down Expand Up @@ -175,19 +155,16 @@ func performOnMainThread(
async: Bool = true,
handler: @escaping @MainActor @Sendable () -> Void
) {
@MainActor(unsafe)
func unsafeHandler() {
handler()
}

if !Thread.isMainThread {
if async {
DispatchQueue.main.async(execute: handler)
} else {
DispatchQueue.main.sync(execute: unsafeHandler)
DispatchQueue.main.sync(execute: handler)
}
} else {
unsafeHandler()
MainActor.assumeIsolated {
handler()
}
}
}

Expand Down Expand Up @@ -416,15 +393,17 @@ private extension FetchedResultsController {
}
return nil
} else {
try fetchAssociatedValues(around: key)
try MainActor.assumeIsolated {
try fetchAssociatedValues(around: key)
}

// On the off chance that the fetch is synchronous, return the new hash value
let holder = associatedValues[key]
return holder?.value
}
}

@MainActor(unsafe)
@MainActor
func fetchAssociatedValues(
around key: AssociatedValueKey<FetchedObject>
) throws {
Expand Down Expand Up @@ -1116,19 +1095,19 @@ private extension FetchedResultsController {
assert(Thread.isMainThread)

memoryPressureToken?.observeIfNeeded { [weak self] notification in
performOnMainThread {
performOnMainThread { [weak self] in
self?.removeAllAssociatedValues()
}
}
definition.objectCreationToken.observeIfNeeded { [weak self] data in
performOnMainThread {
performOnMainThread { [weak self] in
self?.observedObjectUpdate(data)
}
}

for dataResetToken in definition.dataResetTokens {
dataResetToken.observeIfNeeded { [weak self] _ in
performOnMainThread {
performOnMainThread { [weak self] in
self?.handleDatabaseClear()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,9 @@ private class DelegateThunk<FetchedObject: FetchableObject> {
private weak var parent: (any Parent)?
private weak var pausableController: PausableController?

private let willChange: @MainActor (_ controller: PausableController) -> Void
private let didChange: @MainActor (_ controller: PausableController) -> Void

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

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

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)
}
}
}

Expand All @@ -236,14 +216,14 @@ extension DelegateThunk: FetchedResultsControllerDelegate {
return
}
pausableController.objectWillChangeSubject.send()
self.controllerWillChangeContent(pausableController)
self.parent?.controllerWillChangeContent(pausableController)
}

func controllerDidChangeContent(_ controller: ParentController) {
guard let pausableController, !pausableController.isPaused else {
return
}
self.controllerDidChangeContent(pausableController)
self.parent?.controllerDidChangeContent(pausableController)
pausableController.objectDidChangeSubject.send()
}

Expand All @@ -255,7 +235,7 @@ extension DelegateThunk: FetchedResultsControllerDelegate {
guard let pausableController, !pausableController.isPaused else {
return
}
self.controller(pausableController, didChange: object, for: change)
self.parent?.controller(pausableController, didChange: object, for: change)
}

func controller(
Expand All @@ -266,32 +246,32 @@ extension DelegateThunk: FetchedResultsControllerDelegate {
guard let pausableController, !pausableController.isPaused else {
return
}
self.controller(pausableController, didChange: section, for: change)
self.parent?.controller(pausableController, didChange: section, for: change)
}
}

extension DelegateThunk: PausableFetchedResultsControllerDelegate {
public func controllerWillChangeContent(_ controller: PausableController) {
self.willChange(controller)
self.parent?.controllerWillChangeContent(controller)
}

public func controllerDidChangeContent(_ controller: PausableController) {
self.didChange(controller)
self.parent?.controllerDidChangeContent(controller)
}

public func controller(
_ controller: PausableController,
didChange object: FetchedObject,
for change: FetchedResultsChange<IndexPath>
) {
self.changeObject(controller, object, change)
self.parent?.controller(controller, didChange: object, for: change)
}

public func controller(
_ controller: PausableController,
didChange section: FetchedResultsSection<FetchedObject>,
for change: FetchedResultsChange<Int>
) {
self.changeSection(controller, section, change)
self.parent?.controller(controller, didChange: section, for: change)
}
}
4 changes: 2 additions & 2 deletions FetchRequests/Sources/FetchableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public protocol FetchableObjectProtocol: NSObjectProtocol, Identifiable, RawData
static func entityID(from data: RawData) -> ID?

/// Listen for changes to the underlying data of `self`
func observeDataChanges(_ handler: @escaping @MainActor () -> Void) -> InvalidatableToken
func observeDataChanges(_ handler: @escaping @Sendable @MainActor () -> Void) -> InvalidatableToken

/// Listen for changes to whether `self` is deleted
func observeIsDeletedChanges(_ handler: @escaping @MainActor () -> Void) -> InvalidatableToken
func observeIsDeletedChanges(_ handler: @escaping @Sendable @MainActor () -> Void) -> InvalidatableToken

/// Enforce listening for changes to the underlying data of `self`
func listenForUpdates()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private extension InternalFetchResultsControllerProtocol {

public class PaginatingFetchedResultsController<
FetchedObject: FetchableObject
>: FetchedResultsController<FetchedObject> {
>: FetchedResultsController<FetchedObject>, @unchecked Sendable {
private unowned let paginatingDefinition: PaginatingFetchDefinition<FetchedObject>

public init(
Expand Down
Loading

0 comments on commit 6cd2508

Please sign in to comment.