Post

Replies

Boosts

Views

Activity

Any SwiftData change updates every SwiftUI view
Perhaps I just have the wrong expectations, but I discovered some odd behavior from SwiftData that sure seems like a bug to me... If you make any change to any SwiftData model object — even just setting a property to its current value — every SwiftUI view that uses SwiftData is rebuilt. Every query and every entity reference, even if the property was set on a model class that is completely unrelated to the view. SwiftUI does such a good job of optimizing UI updates that it's hard to notice the issue. I only noticed it because the updates were triggering my debug print statements. To double-check this, I went back to Apple's new iOS app template — the one that is just a list of dated items — and added a little code to touch an unrelated record in the background: @Model class UnrelatedItem { var name: String init(name: String) { self.name = name } } @main struct jumpyApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ Item.self, UnrelatedItem.self ]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) do { return try ModelContainer(for: schema, configurations: [modelConfiguration]) } catch { fatalError("Could not create ModelContainer: \(error)") } }() init() { let context = sharedModelContainer.mainContext // Create 3 items at launch so we immediately have some data to work with. if try! context.fetchCount(FetchDescriptor<Item>()) == 0 { for _ in 0..<3 { let item = Item(timestamp: Date()) context.insert(item) } } // Now create one unrelated item. let unrelatedItem = UnrelatedItem(name: "Mongoose") context.insert(unrelatedItem) try? context.save() // Set up a background task that updates the unrelated item every second. Task { while true { try? await Task.sleep(nanoseconds: 1_000_000_000) Task { @MainActor in // We don't even have to change the name or save the contxt. // Just setting the name to the same value will trigger a change. unrelatedItem.name = "Mongoose" } } } } var body: some Scene { WindowGroup { ContentView() } .modelContainer(sharedModelContainer) } } I also added a print statement to the ContentView so I could see when the view updates. struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var items: [Item] var body: some View { NavigationSplitView { List { let _ = Self._printChanges() ... The result is that the print statement logs 2 messages to the debug console every second. I checked in iOS 17, 18.1, and 18.2, and they all behave this way. Is this the intended behavior? I thought the whole point of the new Observation framework in iOS 17 was to track which data had changed and only send change notifications to observers who were using that data.
2
0
401
1w
Module not found error importing framework using SPM package
I'm trying to convert an existing project to use Swift Package Manager projects for its external dependencies. Previously I was using Carthage.My project includes a private framework named HypnoLib, a share extension, and the main application. Both the share extension and the app depend on the private framework. The private framework depends on several SPM packages, one of which is named LlamaLib.The private framework and the app build fine, but when I try to build the share extension I get these errors:&lt;module-includes&gt;:2:9: note: in file included from &lt;module-includes&gt;:2:#import "Headers/HypnoLib-Swift.h" ^/Users/samalone/Library/Developer/Xcode/DerivedData/Succubus-ajafbtomyfjhlgbqdzjavilgugud/Build/Products/Debug-iphonesimulator/HypnoLib.framework/Headers/HypnoLib-Swift.h:185:9: error: module 'LlamaLib' not found@import LlamaLib; ^&lt;unknown&gt;:0: error: could not build Objective-C module 'HypnoLib'Looking at the automatically generated file HypnoLib-Swift.h I see:@import Foundation; @import LlamaLib;but I don't see imports for the other two SPM libraries that are used by HypnoLib.So I guess my questions are:Why does HypnoLib-Swift.h try to import LlamaLib but not the other two SPM packages used in HypnoLib?Why can't LlamaLib be found when my share extension tries to import HypnoLib?
6
0
11k
Jul ’19