I have built a SwiftUI FileDocument app like this.
struct MyApp: App {
var body: some Scene {
DocumentGroup(newDocument: MyFileDocument()) { file in
DocumentView(document: file.$document)
}
}
}
I'm a bit confused how to use the $document Binding.
I know with ObservableObject I can do:
document.objectWillChange
		.sink { _ in
				print("Document changed")
		}
How can I react to changes on $document in my Non-GUI code?
Use case: I'm building an app, where for some changes in the document, there's a command sent over the network. This command should also be sent, if I use e.g. undo. That's why I don't want so send these updates from within my View but as a reaction to a change in $document.
Post
Replies
Boosts
Views
Activity
I've pretty much completed creating my first macOS SwiftUI app. It's working fine but after using it for more than 10 minutes I noticed a dramatic slow down and big memory footprint. I condensed the problem down into the following minimal example, which is a full SwiftUI app for both macOS and iOS.
language Swift
import SwiftUI
@main
struct TestApp: App {
@State var selection: Int?
var body: some Scene {
WindowGroup {
NavigationView {
List(0...10, id: \.self) { element in
NavigationLink(destination: VeryComplexView(text: "\(element)"),
tag: element, selection: $selection) {
Text("\(element)")
}
}
}
}
}
}
struct VeryComplexView: View {
let text: String
var body: some View {
HStack {
ForEach(1...10, id: \.self) { _ in
List(1...5000, id: \.self) { element in
Text("\(text) \(element)")
}
}
}
}
}
On macOS after just a few clicks in the NavigationView I'm getting a memory footprint of 1GB and performance decreases. Profiling the app with Leak Detector also shows a massive amount of leaks in SwiftUI classes. Compiling the same app for iOS stays at a reasonable footprint of 50 MB even after using it for a while. This bug prevents me from properly using my app. I submitted a bug report in Feedback Assistant about a month ago with no answer or reaction. Please help! How can such a huge leak stay unnoticed.
I'm running the latest Big Sur 11.2.1 and Xcode 12.4
Hi there,
I previously experienced a massive memory leak in SwiftUI on macOS which now seems to be fixed. CLICK - https://developer.apple.com/forums/thread/676860
But my app still becomes slower and memory footprint grows after some time of using it. I was able to created a new minimal but more extreme example to reproduce:
This is a full app for SwiftUI. Compile for macOS (I'm using Xcode 12.4 macOS 11.2.3)
import SwiftUI
@main
struct DemoApp: App {
@State var strings = ["Hello 1", "Hello 2"]
@State var bool = false
let timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
@State var selected: Int?
var body: some Scene {
WindowGroup {
List(strings.indices, id: \.self, selection: $selected) { stringIndex in
Text(strings[stringIndex])
}
.toolbar(content: {
ForEach(1...100, id: \.self) { _ in
Text("Hello World")
}
})
.onReceive(timer) { input in
if bool == true { selected = 0 }
else { selected = 1 }
bool = !bool
}
}
}
}
Timer is optional but it will show the problem more quickly. It seems to be connected to toolbar content a lot.
Memory footprint quickly goes up, as well as CPU utilization & app slows down significantly just after a few seconds. Please help! What can I do? My app is pretty much not usable because of this bug.
Johannes