.backgroundTask in SwiftUI cannot compile

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

My Code:

.backgroundTask(.appRefresh("checkValidity")) {
//   scheduleAppRefresh()
//   checkRecords()
}
Post not yet marked as solved Up vote post of LiYanan04818 Down vote post of LiYanan04818
3.0k views
  • Feedback ID: FB10634593

  • 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")
        }
    }
    
Add a Comment

Replies

.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 did that. 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!!!!😔

Add a Comment

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.

Add a Comment

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"

  • Hi, thank you for sharing this. In my case, this doesn't help. Indeed my app is only an iPhone and iPad app, no macOS. Regardless, I tried your code with #if os(iOS) to no avail.

  • Failed too :(

Add a Comment

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.

  • It's not a problem cause by modifier's order. In fact, If I only have one line of code inside WindowGroup, it can compile successfully.

  • Changing the order of .onChange with .backgroundTask worked for me!

Add a Comment

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

  • In order to pack the modified view into one single, unmodified view. My solution is AnyView(ContentView().anyModifiers...). It works for me.  @javiermares

  • @LiYanan04818  thank you for sharing this! I will test it out ASAP. Sorry I took a while to answer, I don't get notifications for mentions in these forums (I'll figure out a way to turn them on)

  • @LiYanan04818 it does work! How did you figure this out?? :D They need to fix this. Again thank you!

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"

  • I totally agree with you. Your assumption is correct.

  • thanks @eskimo ! also .backgroundTask modifier is not sufficient to run the action closure upon app first start, we're having to use workarounds like running the function to schedule the task when the app goes to the background

    @LiYanan04818 do you also have this problem? Don't wanna be off topic so I'll start another thread if need be.

    Thank you both -J.

Add a Comment

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

Add a Comment

+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)
    }
}
  • But I don't want to write like this, it's too wired, although the problem will be fixed. But now, this is the only solution. SO SAD :(

  • Thanks for the work-around. While annoying at least my app compiles now. I've filed a feedback as well: FB11507827.

Add a Comment

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

  • Yep, that matches my expectations. Thanks for checking!

  • Can this be able to run properly on the first public version(iOS 16.0, watchOS 9)?

Add a Comment

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"