.backgroundTask in SwiftUI cannot compile

Error Code: error build: Command CompileSwift failed with a nonzero exit code

My Code:

.backgroundTask(.appRefresh("checkValidity")) {
//   scheduleAppRefresh()
//   checkRecords()
}

Feedback ID: FB10634593

.backgroundTask in SwiftUI cannot compile

If you create a new test project from one of the built-in templates, does that exhibit the same behaviour?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I have the exact same issue. Trying the same on a blank project doesn't produce this error, however, I need my actual project to work and I have no idea what could be causing this. Apple, any guidance please? Is this something you are working to fix? Thank you -J.

It seems like in a blank project, it works. But I have modified the sample code, which is called ComposingCustomLayoutsWithSwiftUI, and it has the same error behavior!!!!

That’s not my experience. Here’s what I did:

  1. On macOS 12.4, I downloaded the Composing custom layouts with SwiftUI sample code.

  2. I opened the CustomLayoutSample.xcodeproj in Xcode 14.0b3.

  3. I got the sample building as is. This involved changing all the code signing to use my Team ID and replacing the body of the Profile view, which currently doesn’t compile, with an EmptyView.

  4. I added the following modifier to the CustomLayoutSampleApp:

    WindowGroup {
        …
    }
    .backgroundTask(.appRefresh("abc")) { _ in
        print()
    }
    
  5. This fails to compile because .appRefresh(…) is not available on macOS, and this is a multi-platform project. So I set it to only apply on iOS:

    WindowGroup {
        …
    }
    #if os(iOS)
    .backgroundTask(.appRefresh("abc")) { _ in
        print()
    }
    #endif
    

With that the project compiles just fine.

I didn’t try to actually run it mind you; my only goal was to get it compiling.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Beta 4 is out. I have tried that again, but unfortunately it fails again.

I checked the docs, .backgroundTask support macOS 13.0+ Beta.

And I also have a question about how to back support iOS 15.0 while using this modifier, because I cannot use if-condition to switch between different WindowsGroups inside the Scene(I got a compile error)

Hello, i had the exact same issue on my machine with Xcode 14 beta 3 and beta 4. What worked for me was to change the order of the modifier This does not compile:

WindowGroup {
  Text("Hello world")
}
.onChange(of: phase) { newPhase in
    // some changes
}
.backgroundTask(.appRefresh("appRefresh")) {
    // async work
}

This does compile:

WindowGroup {
  Text("Hello world")
}
.backgroundTask(.appRefresh("appRefresh")) {
    // async work
}
.onChange(of: phase) { newPhase in
    // some changes
}

I hope this will help.

This can compile:

WindowGroup {
    ContentView() // <-- If we only have one line of code, it works.
}
.backgroundTask(.appRefresh("task")) { 

}

But this can't😒😒😒

WindowGroup {
    ContentView()
         .environment(\.managedObjectContext, persistenceController.container.viewContext) // <-- We have modifier(s) here, it fails.
}
.backgroundTask(.appRefresh("task")) { 

}

Please fix this issue!!!! HELP!!!

@eskimo

Is yours the same?? @javiermares

So far in this thread I’ve assumed that “cannot compile” means “fails to compile with an error”. But I tried your latest test here in my office and I’m seeing the compiler crash. The compiler shouldn’t crash regardless of the state of ones code, so that’s clearly a bug. I’ve filed a report about this myself (r. 97966718).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo, I've filed a feedback for this as well with a very small sample project demonstrating the compiler crash. FB10692950

+1

I have gotten mine to compile now by adding another intermediate view so that .backgroundTask can be on its own. I haven't checked the background task functionality yet, but at least it's compiling. Something like this:

// MyApp.swift

@main
struct MyApp: App {

    var body: some Scene {
        WindowGroup {
            BackgroundTaskView()
        }
        .backgroundTask(.appRefresh("RUN_BACKGROUND")) {
            print("ran background")
        }
    }
}

and then

// BackgroundTaskView.swift

// This used to be in MyApp.swift but caused compilation error

struct BackgroundTaskView: View {
    @StateObject var dataController: DataController

    init() {
        let dataController = DataController()
        _dataController = StateObject(wrappedValue: dataController)
    }

    var body: some View {
        ContentView()
            .environment(\.managedObjectContext, dataController.container.viewContext)
            .environmentObject(dataController)
    }
}

Still not fixed on Xcode Version 14.0 beta 6 (14A5294g) 👎

My project is also not compiling when .backgroundTask is used!

My project still cannot compile on Version 14.0 (14A309)

Now fixed on Version 14.1 beta (14B5024h).

AFAIK this is a compile-time only error, and thus it should work on iOS 16.0. However, I wouldn’t take my word for this. You need to test what you ship.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I also have the same problem. May I ask what the solution is? BackgroundTask still cannot run in blank projects

@main
struct TestApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .backgroundTask(.appRefresh("task")) {
            await update()
        }
    }
    func update() async{
        print("ran background")
    }
}
.backgroundTask in SwiftUI cannot compile
 
 
Q