Skip to content

Commit

Permalink
Move CI to Xcode 15.3, first-pass concurrency-correctness (#23)
Browse files Browse the repository at this point in the history
First pass at Swift concurrency checking.  Problem is that Core Data itself
is not updated.

Remaining problems currently easily unworkaroundable:
* Container metadata [String:Any] — need a Sendable version of this
* NSPC::loadPersistentStores() — the callback semantics need formalizing in the new world

In addition having to @preconcurrency import CoreData in a bunch of places.
  • Loading branch information
johnfairh authored Apr 10, 2024
1 parent fc3efa5 commit db6b1a9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ on:
jobs:
macos:
name: Tests
runs-on: macos-13
env:
DEVELOPER_DIR: /Applications/Xcode_15.0.app/Contents/Developer
runs-on: macos-14
steps:
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 15.3
- uses: actions/checkout@v4
- name: Tests
run: xcodebuild build test -project TMLPersistentContainer.xcodeproj -scheme TMLPersistentContainer-macOS -enableCodeCoverage YES
Expand Down
2 changes: 1 addition & 1 deletion Sources/MigrationError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import CoreData
@preconcurrency import CoreData

/// Errors that can occur preventing persistent store loading, passed into the callback given to
/// `PersistentContainer.loadPersistentStores(...)` or
Expand Down
4 changes: 2 additions & 2 deletions Sources/ModelVersionOrder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import CoreData
/// Note that the *model version* here is the part of its filename before '.xcdatamodel' --
/// *not* the optional 'Model Version Identifier' that you can set in the model's properties panel.
@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public enum ModelVersionOrder {
public enum ModelVersionOrder: Sendable {

/// Compare the model versions directly, but interpreting numbers like a human -- using
/// the `NSString.CompareOptions.numeric` algorithm, meaning that for example `MyModel_v2`
Expand All @@ -41,7 +41,7 @@ public enum ModelVersionOrder {
case pairList([(String,String)])

/// Use a different order for different stores under the same container.
case perStore((NSPersistentStoreDescription) -> ModelVersionOrder)
case perStore(@Sendable (NSPersistentStoreDescription) -> ModelVersionOrder)
}

@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
Expand Down
15 changes: 7 additions & 8 deletions Sources/PersistentContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//

import Foundation
import CoreData
@preconcurrency import CoreData

@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
protocol PersistentContainerProtocol: PersistentContainerMigratable {
Expand Down Expand Up @@ -46,18 +46,16 @@ extension PersistentContainerProtocol {
func loadPersistentStoresHelper(invokeCoreDataClosure: @escaping ((_ block: @escaping (NSPersistentStoreDescription, Error?) -> ()) -> Void), completionHandler block: @escaping (NSPersistentStoreDescription, Error?) -> ()) {
// Filter out the stores that need loading to replicate the superclass API
// There are probably only a handful at most of these so no need to be terribly efficient
let storeURLs = persistentStoreCoordinator.persistentStores.compactMap { $0.url }
let storeURLs = persistentStoreCoordinator.persistentStores.compactMap(\.url)

if storeURLs.count > 0 {
log(.info, "Already have loaded stores associated with container: \(storeURLs)")
}

var storesToLoad: [NSPersistentStoreDescription] = []

persistentStoreDescriptions.forEach { description in
let storesToLoad = persistentStoreDescriptions.filter { description in
guard let storeURL = description.url else {
log(.info, "Not migrating store \(description) because no URL present")
return
return false
}
if !storeURL.isFileURL {
log(.info, "Not migrating store \(description) because not a file:// URL")
Expand All @@ -66,8 +64,9 @@ extension PersistentContainerProtocol {
} else if storeURLs.contains(description.fileURL) {
log(.info, "Not migrating store \(storeURL) because already loaded.")
} else {
storesToLoad.append(description)
return true
}
return false
}

guard storesToLoad.count > 0 else {
Expand All @@ -82,7 +81,7 @@ extension PersistentContainerProtocol {
}

// Helper to deal with the sync/async version....
func doStoreMigration() {
@Sendable func doStoreMigration() {
var failures = false

migrateStores(descriptions: storesToLoad) { desc, error in
Expand Down
2 changes: 2 additions & 0 deletions TMLPersistentContainer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@
SDKROOT = iphoneos;
"SWIFT_ACTIVE_COMPILATION_CONDITIONS[arch=*]" = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down Expand Up @@ -1116,6 +1117,7 @@
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_STRICT_CONCURRENCY = complete;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down

0 comments on commit db6b1a9

Please sign in to comment.