Discuss Swift.

Swift Documentation

Post

Replies

Boosts

Views

Activity

Xcode 16 beta 5 MainActor errors in a Swift 6 project while working in beta 4
After updating to Xcode 16.0 beta 5 (16A5221g) on macOS Sonoma 14.6 I am getting lots of errors like: Main actor-isolated property '[property name]' can not be mutated from a nonisolated context in classes that are marked as @MainActor at the class level, e.g.: @MainActor final class MyClass: AnotherClass { ... } The project uses Swift 6 and there are no errors in Xcode 16 beta 4. I tried restarting, clearing Derived Data, cleaning build folder... Anyone else encountering this issue with beta 5?
1
1
494
Aug ’24
Swiftdata with Date
Dear all, I'm going out of mind with the following issue. I have a view where I'm selecting a date through a datepicker. Then I'm inserting some other data and then I'm trying to save, see private func saveTraining(). But the date which is used to save the training is always one day before the selected date and more than this, I'm not able to save it without time. As I have then a calendar where the saved trainings need to be displayed, I'm not able to match it. Did anybody already face this issue? How can I solve it? I'm reporting here the code of the view where you can also find the checks I put to verify values/ dates: import SwiftData import AppKit struct AddTrainingView: View { @Environment(\.modelContext) private var context @Binding var isAddingTraining: Bool @ObservedObject var season: Season @Binding var selectedDate: Date @State private var trainingStartTime = Date() @State private var trainingEndTime = Date() @State private var trainingConditionalGoal = "" @State private var trainingTacticalGoal = "" @State private var trainingExercises: [TrainingExercise] = [] @State private var showExercisePicker = false @State private var currentTraining: Training? init(isAddingTraining: Binding<Bool>, selectedDate: Binding<Date>, season: Season, currentTraining: Training? = nil) { self._isAddingTraining = isAddingTraining self._selectedDate = selectedDate self.season = season self._currentTraining = State(initialValue: currentTraining) if let training = currentTraining { self._trainingStartTime = State(initialValue: training.startTime) self._trainingEndTime = State(initialValue: training.endTime) self._trainingConditionalGoal = State(initialValue: training.conditionalGoal) self._trainingTacticalGoal = State(initialValue: training.tacticalGoal) self._trainingExercises = State(initialValue: training.trainingExercises) } } var body: some View { VStack(alignment: .leading, spacing: 16) { Text("Aggiungi Allenamento") .font(.title) .bold() .padding(.top) VStack(alignment: .leading) { Text("Data allenamento:") DatePicker("", selection: $selectedDate, displayedComponents: .date) .datePickerStyle(.field) } .padding(.bottom, 10) ... Button("Salva") { saveTraining() isAddingTraining = false dismiss() } .buttonStyle(.borderedProminent) .tint(.blue) Button("Visualizza PDF") { createPDF() } .buttonStyle(.borderedProminent) .tint(.blue) Spacer() } .padding(.bottom) } .padding() .frame(maxWidth: .infinity, maxHeight: .infinity) } private func saveTraining() { // Creiamo un'istanza del calendario corrente var calendar = Calendar.current calendar.timeZone = TimeZone.current // Assicuriamoci di utilizzare il fuso orario locale // Estrarre solo i componenti di data dalla data selezionata let components = calendar.dateComponents([.year, .month, .day], from: selectedDate) // Creiamo una nuova data con i soli componenti di anno, mese e giorno guard let truncatedDate = calendar.date(from: components) else { print("Errore nella creazione della data troncata") return } // Stampa di debug per verificare la data selezionata e quella troncata print("Data selezionata per l'allenamento: \(selectedDate)") print("Data troncata che verrà salvata: \(truncatedDate)") let newTraining = Training( date: truncatedDate, startTime: trainingStartTime, endTime: trainingEndTime, conditionalGoal: trainingConditionalGoal, tacticalGoal: trainingTacticalGoal, season: season ) // Verifica che la data sia correttamente impostata print("Data che verrà salvata: \(newTraining.date)") newTraining.trainingExercises = trainingExercises context.insert(newTraining) do { try context.save() print("Allenamento salvato correttamente.") } catch { print("Errore nel salvataggio: \(error.localizedDescription)") } } private var dateFormatter: DateFormatter { let formatter = DateFormatter() formatter.dateStyle = .short return formatter } private var timeFormatter: DateFormatter { let formatter = DateFormatter() formatter.timeStyle = .short return formatter } } } Example: when I'm selecting 2023-08-21, the debugger retrieves me following data: Data selezionata per l'allenamento: 2023-08-20 22:00:00 +0000 Data troncata che verrà salvata: 2023-08-20 22:00:00 +0000 Data che verrà salvata: 2023-08-20 22:00:00 +0000 Allenamento salvato correttamente.
3
0
658
Aug ’24
include a file with space in its name within xcconfig
Hi, I have a file with a name like this: file name.filetype I need to include it in an xcconfig file. I have read that spaces are used as delimiters in xcconfig files, and I need to correctly quote it. But this is for the framework paths, so not sure if the same applies to config files. This might be a really silly question, but what's the correct quotation format we need to follow? I've tried all the usual stuff: #include "../../../file\ name.filetype" #include "../../../file%20name.filetype" #include "../../../file name.filetype" #include "../../../file"+" "+"name.filetype" But neither is working. Am I having a major brain meltdown moment or is there a specific way to do it? Thanks a lot.
0
0
205
Aug ’24
How to inheritance Cocoapods Library to extend the functionality in the Xcode framework project
When building my iOS framework, I encountered a (fatal) module 'TrustKit' not found issue. I've marked the necessary classes as public (which is inherited from other library classes) for client applications, but I'm unsure how to resolve this error. Example Code Snippet import Foundation import TrustKit @objc public class InheritanceTruskitFrameworkProjectClass: TrustKit { func extraFunctionality() { print("extraFunctionality executing...") } } Error while building the framework in the auto-generated Swift header file (ProjectName-Swift.h) #if !defined(SWIFT_IMPORT_STDLIB_SYMBOL) # define SWIFT_IMPORT_STDLIB_SYMBOL #endif #endif #if defined(__OBJC__) #if __has_feature(objc_modules) #if __has_warning("-Watimport-in-framework-header") #pragma clang diagnostic ignored "-Watimport-in-framework-header" #endif @import Foundation; @import TrustKit; # -> error here `(fatal) module 'TrustKit' not found` #endif #endif #pragma clang diagnostic ignored "-Wproperty-attribute-mismatch" #pragma clang diagnostic ignored "-Wduplicate-method-arg" #if __has_warning("-Wpragma-clang-attribute") # pragma clang diagnostic ignored "-Wpragma-clang-attribute" #endif #pragma clang diagnostic ignored "-Wunknown-pragmas" #pragma clang diagnostic ignored "-Wnullability" #pragma clang diagnostic ignored "-Wdollar-in-identifier-extension" Please check this image to understand the error Sample GitHub project https://github.com/vinaykumar0339/InheritanceTruskitFrameworkProject I want a way to use inheritance for the dependencies project. Is this supported? Looks Like it won't work with any modules like cocopod frameworks, internal module map etc? What I have Tried I have tried to install TrustKit with SPM also the same error throwing (fatal) module 'TrustKit' not found
0
0
366
Aug ’24
Ending Live Activities When Timer Expires
Hi everyone, I'm working on an app that uses the Screen Time API, and I'm encountering an issue with Live Activities. The app runs a timer (e.g., 10 minutes), after which the intervalDidEnd method in DeviceActivityMonitor is triggered. To improve the user experience, I've implemented Live Activities to display the remaining time. However, I'm having trouble stopping the Live Activity when the timer expires and intervalDidEnd is called. It seems that the Screen Time extensions cannot detect or interact with active Live Activities, even though both share the same App Group. My Question: Since the DeviceActivityMonitor extension does not seem to be able to detect Live Activities, does anyone know if there’s a way to end a Live Activity when the timer expires without relying on a server? Using Apple’s Push Notification service feels excessive given the lightweight nature of the app, which doesn’t use server-side components. Any insights or suggestions would be greatly appreciated! Thanks!
1
0
418
Aug ’24
Embedding an xcframework in another xcframework.
Anytime I try to embed my xcframework (which has another xcframework embedded in it) in an iOS app and run it on a device I get this error in the console : dyld[5028]: Library not loaded: @rpath/Calculator.framework/Calculator   Referenced from:... It runs fine on the simulator however. I've tried several suggested solutions; modifying Runpath Search Paths, Framework Search Paths but to no avail. Is there something I'm missing or you can't have nested frameworks for iOS as per this question? If so why does it run on the simulator though.
2
0
1.5k
Apr ’22
The Mystery of an Array of Strings and Escaped Characters
I have a very simple set of lines of code. It doesn't matter whether you run it under UIKit or SwiftUI. In SwiftUI, I have the following. import SwiftUI struct ContentView: View { var body: some View { VStack { Button("Click on me") { let tabLine = "1\tAnthony James\t139.9" var item = "" let tabs = tabLine.components(separatedBy: "\t") for tab in tabs { item += "'\(tab)'" } print("\(item)") } } } } So I have tab-separated values. And I want to separate them and quote each value either with an apostrophe or a double quotation mark. In the case above, I get the following print. '1''Anthony James''139.9' That's exactly what I want. Now, I have an array of three of those guys like the following. import SwiftUI struct ContentView: View { var body: some View { VStack { Button("Click on me") { let tabLine0 = "1\tAnthony James\t139.9" let tabLine1 = "2\tKim Harbaugh\t181.4" let tabLine2 = "3\tAnthony James\t212.4" let tabTextLines = [tabLine0, tabLine1, tabLine2] var strings = [String]() for tabLine in tabTextLines { var item = "" let tabs = tabLine.components(separatedBy: "\t") for tab in tabs { item += "'\(tab)'" } strings.append(item) } print("\(strings)") } } .frame(width: 360, height: 240) } } And I get the following print. This is a nightmare situation. Each value is quoted with an escaped apostrophe. I can't even remove the escapees with replacingOccurrences(of:with:). How does that happen when you have an array of strings? If I try quoting the values with a unicode character, things are the same. Is there a workaround? Muchos thankos.
4
0
296
Aug ’24
Incorrect Decimal Init Behavior
With the release of iOS 18 Developer Beta 5 and Public Beta 3, my team received reports from customers that all negative values in our app were showing as positives, which led to major confusion. We narrowed down the issue to a change to the initializer for Decimal: Decimal(sign:exponent:significand). Prior to Beta 5, the sign passed into the initializer would be the sign of the decimal. Passing plus would result in a positive decimal, and passing minus would result in a negative decimal. This is regardless of the sign of the significant. In Beta 5, it seems that the sign passed into the init, and the sign of the significand are now multiplied together. This means that passing .minus for sign and a negative Decimal for significand results in an unexpectedly positive Decimal value in Beta 5 alone. This behavior does not seem to be explicitly documented. I created a quick playground to illustrate the issue. Here's the code: // Expected Value: 20 // Actual Value: 20 let positiveDecimal = Decimal(sign: .plus, exponent: 1, significand: 2) // Expected Value: 20 // Actual Value (15.4.0, 16.0 Beta 4): 20 // Actual Value (16.0 Beta 5): -20 let positiveDecimalWithNegativeSignificand = Decimal(sign: .plus, exponent: 1, significand: -2) // Expected Value: -20 // Actual Value: -20 let negativeDecimal = Decimal(sign: .minus, exponent: 1, significand: 2) // Expected Value: -20 // Actual Value (15.4.0, 16.0 Beta 4): -20 // Actual Value (16.0 Beta 5): 20 let negativeDecimalWithNegativeSignificand = Decimal(sign: .minus, exponent: 1, significand: -2) Here is the result of running the playground in Xcode 16.0 Beta 4 (and 15.4.0): And here is the result of running it in Xcode 16.0 Beta 5: I've tracked down the issue to a PR in the swift-foundation repo from 3 weeks ago, which seems to pull a commit from the swift-corelibs-foundation repo. For anyone else who finds this thread looking for a solution, in the mean time you can wrap the significant in abs and this should ensure consistent behavior between iOS 18 Beta 5 and prior iOS versions.
2
0
391
Aug ’24
Codable with superclass doesn't work ??
class A: Codable { var x: String? } class B: A { var y: String? } When I try to decode class B, the property "y" remains nil even though there is a value for it in the JSON data. If I change it to B: Codable then it works. When I try to search for a solution, all I can find is people overriding init methods and doing decoding manually in some way. I am having a hard time believeing that this is the right answer. Surely, coding works across class hierarchies?
3
0
252
Aug ’24
Opening an App Conditionally with Swift and App Intents in iOS Shortcuts
I am trying to create a simple app that "blocks" other apps if a certain condition is not met. I am currently using the IOS shortcuts and have set up an automation that opens my app A whenever another app B opens. If the condition is not met i imagine the flow to look like: Open app A. My app B opens instead. I check a box in my app B. I navigate back to app A and it works as expected. If the condition already is met the app A would work as expected from the beginning. What is have tried so far My first attempt involved using an AppIntent and changing the openAppWhenRun programmatically based on the condition. I did however learn pretty quickly that changing the value of openAppWhenRun does not change if the AppIntent actually opens my app. The code for this looked like this where the value of openAppWhenRun is changed in another function. struct BlockerIntent: AppIntent { static let title: LocalizedStringResource = "Blocker App" static let description: LocalizedStringResource = "Blocks an app until condition is met" static var openAppWhenRun: Bool = false @MainActor func perform() async throws -> some IntentResult { return .result() } } Another attempt involved setting openAppWhenRun to false in an outer AppIntent and opening another inner AppIntent if the condition is met. If the condition in my app is met openAppWhenRun is set to true and instead of opening the inner AppIntent an Error is thrown. This functions as expected but there is an error notification showing every time I open the "blocked" app. struct BlockerIntent: AppIntent { static let title: LocalizedStringResource = "Blocker App" static let description: LocalizedStringResource = "Blocks an app until condition is met" static var openAppWhenRun: Bool = false func perform() async throws -> some IntentResult & OpensIntent { if (BlockerIntent.openAppWhenRun) { throw Error.notFound } return .result(opensIntent: OpenBlockerApp()) } enum Error: Swift.Error, CustomLocalizedStringResourceConvertible { case notFound var localizedStringResource: LocalizedStringResource { switch self { case .notFound: return "Ignore this message" } } } } struct OpenBlockerApp: AppIntent { static let title: LocalizedStringResource = "Open Blocker App" static let description: LocalizedStringResource = "Opens Blocker App" static var openAppWhenRun: Bool = true @MainActor func perform() async throws -> some IntentResult { return .result() } } My third attempt look similar to the previous one but instead I used two different inner AppIntents. The only difference between the two were that on had openAppWhenRun = false and the other had openAppWhenRun = true. struct BlockerIntent: AppIntent { static let title: LocalizedStringResource = "Blocker App" static let description: LocalizedStringResource = "Blacks an app until condition is met" static var openAppWhenRun: Bool = false func perform() async throws -> some IntentResult & OpensIntent { if (BlockerIntent.openAppWhenRun) { return .result(opensIntent: DoNotOpenBlockerApp()) } else { return .result(opensIntent: OpenBlockerApp()) } } } Trying this gives me this error: Function declares an opaque return type 'some IntentResult & OpensIntent', but the return statements in its body do not have matching underlying types I have also tried opening the app with a URL link with little to no success often ending up in an infinity loop, I did try the ForegroundContinuableIntent but it did not function as expected since it relies on the users input. Is there any way to do what I am trying to accomplish? I have seen other apps using a similar concept so I feel like this should be possible. Many thanks!
5
5
1.5k
Nov ’23
SwiftData iOS18: "Could not materialize Objective-C class named "Set" from declared attribute value type "Set<String>" of attribute named..."
Posting here to see if folks have workarounds or if I have a misunderstanding of SwiftData supported types. In adopting SwiftData, I have swiftData properties of collection type (Array or Set - both have this issue). E.g: @Model final class Item { var timestamp: Date var strings = ["aa", "bb"] var display: String { strings.joined(separator: " ") } init(timestamp: Date) { self.timestamp = timestamp } } So far in development I haven't had issues on iOS 17, but on the iOS 18 betas 4-5 the app logs show the following error: "fault: Could not materialize Objective-C class named "Array" from declared attribute value type "Array<String>" of attribute named strings" It happens immediately in my app when creating an object with a collection attribute. In a minimal test example, the error log appears only after a few minutes and doesn't seem to affect the template app's basic functionality. Anyone else running into this? Was filed as FB14397250
5
9
959
Aug ’24
Getting error while creating identity from certificate and key
I am trying to create identity from certificate and private key which are in base64 format. I am getting error - Unable to create identity one time I get the error - Failed to add certificate and private key to keychain: -26276 My Xcode is 15.3 and macOS is Sonoma 14.5 func loadIdentity(certificate: String, privateKey: String) -> SecIdentity? { guard let certData = Data(base64Encoded: certificate) else { print("Unable to encode certificate base64") return nil } guard let cert = SecCertificateCreateWithData(nil, certData as CFData) else { print("Unable to create certificate") return nil } let certAddQuery: [NSString: Any] = [ kSecClass: kSecClassCertificate, kSecValueRef: cert, kSecAttrLabel: "myCertificate" ] var status = SecItemAdd(certAddQuery as CFDictionary, nil) if status != errSecSuccess && status != errSecDuplicateItem { print("Failed to add certificate to keychain: \(status)") return nil } guard let keyData = Data(base64Encoded: privateKey) else { print("Unable to encode private key base64") return nil } let keyDict: [NSString: Any] = [ kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeyClass: kSecAttrKeyClassPrivate, kSecAttrKeySizeInBits: 2048, kSecReturnPersistentRef: true ] var error: Unmanaged<CFError>? guard let privateKeyData = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, &error) else { print("Unable to create private key") return nil } let keyAddQuery: [NSString: Any] = [ kSecClass: kSecClassKey, kSecValueRef: privateKeyData, kSecAttrLabel: "myKey", kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked ] status = SecItemAdd(keyAddQuery as CFDictionary, nil) if status != errSecSuccess && status != errSecDuplicateItem { print("Failed to add private key to keychain: \(status)") return nil } let identityQuery: [NSString: Any] = [ kSecClass: kSecClassIdentity, kSecReturnRef: true, kSecAttrLabel: "myCertificate", kSecMatchItemList: [cert, privateKeyData] ] var identity: CFTypeRef? status = SecItemCopyMatching(identityQuery as CFDictionary, &identity) guard status == errSecSuccess else { print("Unable to create identity") return nil } return (identity as! SecIdentity) }
0
0
176
Aug ’24
how to clear a TextField when it is bound to a Swiftdata object that doesn't allow optionals
Say you have a SwiftData object that doesn't allow optionals: @Model class MySet: Identifiable { var id: UUID var weight: Int var reps: Int var isCompleted: Bool var exercise: Exercise Then you get from your MySet data a list of these MySets and append them into a State: @State var sets: [MySet] if let newSets = exercise.sets { //unwrapping the passed exercises sets if (!newSets.isEmpty) { //we passed actual sets in so set them to our set sets = newSets } And you use that State array in a ForEach and bind the Textfield directly to the data like so: ForEach($sets){ $set in TextField("\(set.weight)", value: $set.weight, formatter: NumberFormatter()) .keyboardType(.decimalPad) TextField("\(set.reps)", value: $set.reps,formatter: NumberFormatter()) .keyboardType(.decimalPad) } This will bind whatever is written into the TextField directly to the SwiftData object which is a problem because say you have 50 written and you want to change it to 60 you need to be able to clear the 50 to write the 60 and since the SwiftData object doesn't allow nil it will allow you to remove the 0 but not the 5. Is there anyway to remedy this without having the swiftData object allow optionals for example catching the clearing of the TextField and making it 0 instead of nil when its cleared?
0
0
290
Aug ’24
Task Isolation Inheritance and SwiftUI
This post discusses a subtlety in Swift concurrency, and specifically how it relates to SwiftUI, that I regularly see confusing folks. I decided to write it up here so that I can link to it rather than explain it repeatedly. If you have a question or a comment, start a new thread and I’ll respond there. Put it in the App & System Services > Processes & Concurrency topic area and tag it with both Swift and Concurrency. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Task Isolation Inheritance By default, tasks inherit their actor isolation from the surrounding code. This is a common source of confusion. My goal here is to explain why it happens, why it can cause problems, and how to resolve those problems. Imagine you have a main actor class like this: @MainActor class MyClass { var counter: Int = 0 func start() { Task { print("will sleep") doSomeCPUIntensiveWork() print("did sleep") } } } In this example the class is a model object of some form, but it could be an @Observable type, a SwiftUI view, a UIKit view controller, and so on. The key thing is that the type itself is isolated to the main actor. Remember that Swift code inherits its isolation from the surrounding code (in compiler author speak this is called the lexical context). So the fact that MyClass is annotated with @MainActor means that both counter and start() are isolated to the main actor. IMPORTANT This model is what allows the compiler to detect concurrency problems at compile time. I’ve found that, whenever I’m confused by Swift concurrency, it helps to ask myself “What does the compiler know?” Folks look at this code and think “But I’ve added a Task, and that means that doSomeCPUIntensiveWork() will run on a secondary thread!” That is not true. There are a couple of easy ways to prove this to yourself: Actually run the code. If you put this code into an app, you’ll find that your app’s UI is unresponsive for the duration of the doSomeCPUIntensiveWork(). Indeed, you can test this example for yourself, as explained below in Example Context. Access a value that’s isolated to the main actor. For example, insert this doSomeCPUIntensiveWork(): self.counter += 1 doSomeCPUIntensiveWork() The compiler doesn’t complain about this access to counter — a main-actor-isolated value — from this context, which tell you that this code will run on the main thread. So, what’s going on? The task is running on the main actor because of a form of isolation inheritance. The mechanics of that are complex, something I’ll explained in the The Gory Details section below. For the moment, however, the key thing to note is that starting a task in this way — using Task.init(…) — causes the task to inherit actor isolation from the surrounding code. In this case the surrounding code is the start() method, which is isolated to the main actor because it’s part of MyClass, and thus the code ends up calling doSomeCPUIntensiveWork() on the main thread. So, how do you prevent this? There are many different ways, but the two most obvious are: Replace Task.init(…) with Task.detached(…): func start() { Task.detached() { print("will sleep") doSomeCPUIntensiveWork() print("did sleep") } } And how does that work? Again, see the The Gory Details section below. Move the code to a non-isolated method: func start() { Task { print("will sleep") await self.myDoSomeCPUIntensiveWork() print("did sleep") } } nonisolated func myDoSomeCPUIntensiveWork() async { doSomeCPUIntensiveWork() } In both cases you can prove to yourself that this has done the right thing: Add code to access counter from the non-isolated context and observe the complaints from the compiler. SwiftUI While my “What does the compiler know?” thought experiment is super helpful, sometimes it’s not easy understand that. Folks are often caught out by the way that the SwiftUI View protocol works. We’ve fixed this problem in Xcode 16, but that change has brought more confusion. In Xcode 15 and earlier the View protocol was defined like this: public protocol View { … @ViewBuilder @MainActor var body: Self.Body { get } } Only the body property is annotated with @MainActor. The view as a whole is not. Consider this view: struct CounterViewOK: View { @State var counter: Int = 0 var body: some View { VStack { Text("\(counter)") Button("Increment") { Task { counter += 1 } } } } } This compiles because the task inherits the main actor isolation from body. But if you make a seemingly trivial change, the compiler complains: struct CounterViewNG: View { @State var counter: Int = 0 var body: some View { VStack { Text("\(counter)") Button("Increment") { increment() } } } func increment() { Task { counter += 1 // ^ Capture of 'self' with non-sendable type 'CounterViewNG' in a `@Sendable` closure } } } That’s because the increment() method is not isolated to the main actor, and thus neither is the task. The compiler thinks you’re trying to pass an instance of the view between contexts, and rightly complains. In contrast, in Xcode 16 (currently in beta) the View protocol looks ilke this: @MainActor @preconcurrency public protocol View { … @ViewBuilder @MainActor @preconcurrency var body: Self.Body { get } } The entire View is now isolated to the main actor. This makes everything easier to understand. Both of the examples above work. Specifically, CounterViewNG works because the task inherits main actor isolation via the increment() > CounterViewNG > View chain. Of course, everything is a trade-off. More of your views are now running on the main actor, which can trigger the CPU intensive work issue that I described above. Other Options When I crafted the doSomeCPUIntensiveWork() example above, I avoided mentioning SwiftUI. There was a specific reason for that: When working with a UI framework, it’s best to avoid doing significant work in your UI types. This is true in SwiftUI, but it’s also true in UIKit and AppKit. Indeed, doing all your app’s work in your view controllers is called the massive view controller anti-pattern. So, if you’re find yourself doing significant work in your UI types, consider some alternatives. You have lots of options: The simplest option is to move the code into an async function. But you might also want to add an abstraction layer. Swift has lots of good options for that (structs, enums, classes, actors). You can also define a new global actor. The best option depends on your specific situation. If you’re looking for further advice, there’s no shortage of it out there on the ’net (-: The Gory Details To understand the difference between Task.init(…) and Task.detached(…), you have to look at their declarations. This is easy to do from Xcode — just command-click on the init or the detached — but that’s misleading. The difference is keyed off a underscore-prefixed attribute and, for better or worse, Xcode won’t show you those. To see the actual difference you have have to open the Swift interface file. Within any given SDK the relevant file is usr/lib/swift/_Concurrency.swiftmodule/arm64e-apple-macos.swiftinterface. Here’s what you’ll see in the macOS SDK within Xcode 16.0b4: @discardableResult @_alwaysEmitIntoClient public init( priority: TaskPriority? = nil, @_inheritActorContext @_implicitSelfCapture operation: __owned @escaping @isolated(any) @Sendable () async -> Success ) {…} @discardableResult @_alwaysEmitIntoClient public static func detached( priority: TaskPriority? = nil, operation: __owned @escaping @isolated(any) @Sendable () async -> Success ) -> Task<Success, Failure> {…} Note I’ve edited this significantly to make things easier to read. The critical difference is the use of @_inheritActorContext in the Task.init(…) case. This tells the compiler that the closure argument should inherit its isolation from the surrounding code. This attribute is underscored, and thus there’s no Swift Evolution proposal for it, but there is some limited documentation. Example Context To run the example in context, create a new command-line tool project, rename main.swift to start.swift, and insert MyClass into this scaffolding: import Foundation @MainActor class MyClass { … code above … } func doSomeCPUIntensiveWork() { sleep(5) } @main struct Main { static func main() { Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in print("tick") } let m = MyClass() m.start() withExtendedLifetime(m) { RunLoop.current.run() } } } In this context: doSomeCPUIntensiveWork() uses the sleep system call to hog the current thread for 5 seconds. The timer tick helps illustrate the unresponsive main thread. It’s also need to ensure that the run loop continues to run indefinitely. More Reading There is a lot of good information available about Swift concurrency. My favourite resources include: The Swift Programming Language > Concurrency Migrating to Swift 6 The Avoid hangs by keeping the main thread free from non-UI work section of Improving app responsiveness WWDC 2023 Session 10248 Analyze hangs with Instruments, especially the section starting at 31:42. Swift Evolution proposals SE-0431 @isolated(any) Function Types which covers another subtle issue with tasks Matt Massicotte blog at https://www.massicotte.org Revision History 2024-08-05 Added the Other Options section. Added some more links to the More Reading section. Made other minor editorial changes. 2024-08-01 First posted.
0
0
1.2k
Aug ’24
swift: documentation for proper import name?
This bites me a lot. I'm looking at the documentation for, say, UNUserNotificationCenter. And NOWHWERE but NOWHERE do I see anything that says, "hey, on platform *** you should import YYY to use this class." Am I just not looking in the right place in Apple documentation to find this? Surely, somewhere at the top level of documentation, it must tell you want the proper package to import is, per platform?
1
0
338
Aug ’24
Porting Thread & Delegate Code to Swift 6 Using Tasks
Hello, I have a lot of apps and I am currently trying to port them over to Swift 6. I thought that this process should be relatively simple but I have to admit that I have a lot of trouble to understand how the Concurrency system works. Let's start with some code that shows how I am currently working when it comes to asynchronous work in my apps: I have a Model that is marked with @Observable. Inside this model, a Controller is hosted. The Controller has its own ControllerDelegate. The Model has a search function. Inside this function a lot of IO stuff is executed. This can take a lot of time. Because of this fact, I am doing this in a separate Thread. I all is put together, it looks a little bit like this: @main struct OldExampleApp : App { @State private var model = Model() var body: some Scene { WindowGroup { ContentView() .environment(self.model) } } } struct ContentView: View { @Environment(Model.self) private var model var body: some View { if self.model.isSearching { ProgressView() } else { Button("Start") { self.model.search() } } } } protocol ControllerDelegate : AnyObject { func controllerDidStart() func controllerDidEnd() } class Controller { weak var delegate: ControllerDelegate? func search() { let thread = Thread { DispatchQueue.main.async { self.delegate?.controllerDidStart() } // Do some very complex stuff here. Let's use sleep to simulate this. Thread.sleep(forTimeInterval: 2.0) DispatchQueue.main.async { self.delegate?.controllerDidEnd() } } thread.start() } } @Observable class Model { private(set) var isSearching = false var controller = Controller() init() { self.controller.delegate = self } func search() { self.controller.search() } } extension Model : ControllerDelegate { func controllerDidStart() { self.isSearching = true } func controllerDidEnd() { self.isSearching = false } } This works perfectly fine and by that I mean: The task is run in the background. The main thread is not blocked. The main window can be dragged around, no beach ball cursor etc. Now comes the Swift 6 part: I want to merge the Model and Controller into one class (Model). I still want the Model to be Observable. I want to run arbitrary code in the Model. This means that the code is not necessarily a prime candidate for await like getting data from a web server etc. The main thread should not be blocked, so the main window should still be movable while the app calculates data in the background. I have this example: struct ContentView: View { @Environment(Model.self) private var model var body: some View { if self.model.controller.isSearching { ProgressView() } else { Button("Search") { Task { await self.model.controller.heavyWork() } } } } } @Observable final class Model : Sendable { @MainActor var controller = AsyncController() init() { } } @Observable @MainActor class AsyncController { private(set) var isSearching = false public func heavyWork() async { self.isSearching = true Swift.print(Date.now) let i = self.slowFibonacci(34) Swift.print(i) Swift.print(Date.now) self.isSearching = false } func slowFibonacci(_ n: Int) -> Int { if n <= 1 { return n } let x = slowFibonacci(n - 1) let y = slowFibonacci(n - 2) return x + y } } I come from a C# background and my expectation is that when I use a Task with await, the main thread is not blocked and the Code that is called inside the Task runs in the background. It seems like the function is run in the background, but the UI is not updated. Because I set the isSearching flag to true, I would expect that the app would display the ProgressView - but it does not. I changed the code to this: public func heavyWork() async { self.isSearching = true Swift.print(Date.now) let i = await self.slowFibonacci(20) Swift.print(i) Swift.print(Date.now) self.isSearching = false } func slowFibonacci(_ n: Int) async -> Int { let task = Task { () -> Int in if n <= 1 { return n } let x = await slowFibonacci(n - 1) let y = await slowFibonacci(n - 2) return x + y } return await task.value } This seems to work - but is this correct? I have this pattern implemented in one of my apps and there the main thread is blocked when the code is run. So I think it all comes down to this: Is it possible, to run a arbitrary code block (without an await in it) in a Task, that can be awaited so the main thread is not blocked? The class (or actor?) that contains the function that is called via await should be Observable. Or should I simply keep my Swift 5 code and move on? :D Regards, Sascha
3
0
565
Jul ’24
Regression in Concurrent Task Execution on macOS 15 Beta: Seeking Clarification
Developer Community, I've noticed a significant change in concurrent task execution behavior when testing on macOS 15 beta 4 &amp;amp; Xcode 16 Beta 4 compared to previous versions. Tasks that previously ran concurrently now appear to execute sequentially, impacting performance and potentially affecting apps relying on concurrent execution. To illustrate this, I've created a simple toy example: import SwiftUI struct ContentView: View { @State private var results: [String] = [] var body: some View { VStack { Button("Run Concurrent Tasks") { results.removeAll() runTasks() } ForEach(results, id: \.self) { result in Text(result) } } } func runTasks() { Task { async let task1 = countingTask(name: "Task 1", target: 1000) async let task2 = countingTask(name: "Task 2", target: 5000) async let task3 = countingTask(name: "Task 3", target: 1500) let allResults = await [task1, task2, task3] results = allResults } } func countingTask(name: String, target: Int) async -&amp;gt; String { print("\(name) started") var count = 0 for _ in 0..&amp;lt;target { count += 1 } print("\(name) finished. Count: \(count)") return "\(name) completed. Count: \(count)" } } Observed behavior (macOS 15 Beta 4 &amp;amp; Xcode 16 Beta 4): Tasks appear to execute sequentially: Task 1 started Task 1 finished. Count: 1000 Task 2 started Task 2 finished. Count: 5000 Task 3 started Task 3 finished. Count: 1500 Expected behavior: Tasks start almost simultaneously and finish based on their workload: Task 1 started Task 2 started Task 3 started Task 1 finished. Count: 1000 Task 3 finished. Count: 1500 Task 2 finished. Count: 5000 Observed behavior in macOS 15 Beta: The profile reveals that the tasks are executing sequentially. This is evidenced by each task starting only after the previous one has completed.
3
0
585
Jul ’24
"Passing argument of non-sendable type 'ContentView' outside of main actor-isolated context may introduce data races"
I'm currently in the process of migrating to Swift 6. A lot of my code triggers the warning from the title. Passing argument of non-sendable type 'ContentView' outside of main actor-isolated context may introduce data races. I depend on the .task/.refreshable modifiers and buttons that trigger asynchronous work that cannot be done on the Main Actor since it takes way to long. The below code demonstrates the problem. Some comments explain my problems further. I read a lot of articles and documentations but couldn't find an answer to such a seemingly simple error struct ContentView: View { // Marking Senable as suggested by the warning causes different warning for @State @State private var authorizationStatus: MusicAuthorization.Status = .notDetermined // Sole purpose to trigger the errors var body: some View { VStack { Text("Hello, world!") Button("Some button") { Task { await doingSomeAsyncWork() // WARNING: Passing argument of non-sendable type 'ContentView' outside of main actor-isolated context may introduce data races } } } .task { // Or refreshable I believe both behave the same await doingSomeAsyncWork() // WARNING: Passing argument of non-sendable type 'ContentView' outside of main actor-isolated context may introduce data races } } // Marking @MainActor is not an option since some of these functions might be running for more than 10 seconds // Tried marking func as nonisolated but that obviously had no effect func doingSomeAsyncWork() async { authorizationStatus = await MusicAuthorization.request() // Just to have a easy asynchronous function. Without some async code in here, the errors disappear } } Thank you
1
0
1.4k
Jul ’24