Post

Replies

Boosts

Views

Activity

Reply to SwiftData ModelContext fails to delete all model instances from unit tests.
Did you try to save the model context after doing the deletion? When using a model context created with ModelContext(modelContainer), you need to save the changes explicitly because the auto-save doesn't come to play, which is different from when you use mainContext. Hmm… I can give that a try. My question then would be is why do these two functions: extension Store { public func delete<T>(model: T.Type) throws where T : PersistentModel { try self.modelContext.delete(model: model) } } extension Store { public func deleteWithIteration<T>(model: T.Type) throws where T : PersistentModel { for model in try self.fetch(model) { self.modelContext.delete(model) } } } Seem to show different behavior without an explicit save being called? Calling the delete function (with no explicit save) returns with no error thrown and no models have been deleted. Calling the deleteWithIteration function (with no explicit save) returns with no error thrown and all models have been deleted. Should those two functions not return with the same state (either both functions delete all models or both functions delete no models)?
Aug ’24
Reply to How to debug this?
With Xcode, you can use -com.apple.CoreData.ConcurrencyDebug 1 as a launch argument to do the check. By any chance do we know if there are any known issues that might lead to false positive errors when using the ConcurrencyDebug argument with SwiftData? I am seeing some concurrency errors with a SwiftData stack… but I'm looking through how this stack is set up and I can't understand what could be leading to any kind of race condition. Ahh… I don't know how to delete this comment and move it to a thread. Sorry about that!
Topic: UI Frameworks SubTopic: SwiftUI
Jul ’24
Reply to SwiftData and command line
import Foundation import SwiftData @Model class Person { var name: String init(name: String) { self.name = name } } func main() { let configuration = ModelConfiguration( isStoredInMemoryOnly: true, allowsSave: true ) do { let _ = try ModelContainer( for: Person.self, configurations: configuration ) } catch { print(error) } } main() I'm able to build and run by passing true to allowsSave. Is passing false from CLI not supported? I'm on Xcode Version 15.2 (15C500b).
Jan ’24
Reply to SwiftUI.View Compiler Errors when Property Wrapper is Annotated with MainActor
I can also fix the compiler error by putting the App conformance directly on the extension where my body is defined: @main struct MainActorDemoApp { // var body: some Scene { // WindowGroup { // ContentView() // } // } } extension MainActorDemoApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @Wrapper var value = "Hello, world!" var body: some View { Text(self.value) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’24
Reply to SwiftData and command line
import Foundation import SwiftData @Model class Person { var name: String init(name: String) { self.name = name } } func main() { print(Bundle.main.bundleIdentifier ?? "") // com.northbronson.DataDemo print(Bundle.main.object(forInfoDictionaryKey: "CFBundleName") ?? "") // DataDemo let configuration = ModelConfiguration( isStoredInMemoryOnly: true, allowsSave: false ) print(configuration.url) // file:///dev/null // let _ = configuration.url.startAccessingSecurityScopedResource() do { let _ = try ModelContainer( for: Person.self, configurations: configuration ) } catch { // configuration.url.stopAccessingSecurityScopedResource() print(error) } } main() // com.northbronson.DataDemo // DataDemo // file:///dev/null // error: addPersistentStoreWithType:configuration:URL:options:error: returned error NSCocoaErrorDomain (257) // CoreData: error: addPersistentStoreWithType:configuration:URL:options:error: returned error NSCocoaErrorDomain (257) // error: userInfo: // CoreData: error: userInfo: // error: NSFilePath : /dev/null // CoreData: error: NSFilePath : /dev/null // error: storeType: SQLite // CoreData: error: storeType: SQLite // error: configuration: (null) // CoreData: error: configuration: (null) // error: URL: file:///dev/null/ // CoreData: error: URL: file:///dev/null/ // CoreData: annotation: options: // CoreData: annotation: NSMigratePersistentStoresAutomaticallyOption : 1 // CoreData: annotation: NSPersistentHistoryTrackingKey : 1 // CoreData: annotation: NSInferMappingModelAutomaticallyOption : 1 // CoreData: annotation: NSPersistentStoreRemoteChangeNotificationOptionKey : 1 // CoreData: annotation: NSReadOnlyPersistentStoreOption : 1 // error: <NSPersistentStoreCoordinator: 0x600000960180>: Attempting recovery from error encountered during addPersistentStore: 0x600002960f00 Error Domain=NSCocoaErrorDomain Code=257 "The file “null” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/dev/null} // CoreData: error: <NSPersistentStoreCoordinator: 0x600000960180>: Attempting recovery from error encountered during addPersistentStore: 0x600002960f00 Error Domain=NSCocoaErrorDomain Code=257 "The file “null” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/dev/null} // error: Store failed to load. <NSPersistentStoreDescription: 0x600002960db0> (type: SQLite, url: file:///dev/null/) with error = Error Domain=NSCocoaErrorDomain Code=257 "The file “null” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/dev/null} with userInfo { // NSFilePath = "/dev/null"; // } // CoreData: error: Store failed to load. <NSPersistentStoreDescription: 0x600002960db0> (type: SQLite, url: file:///dev/null/) with error = Error Domain=NSCocoaErrorDomain Code=257 "The file “null” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/dev/null} with userInfo { // NSFilePath = "/dev/null"; // } // Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=257 "The file “null” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/dev/null} // SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer) // Program ended with exit code: 0 Was anyone able to get this working with a ModelConfiguration set to isStoredInMemoryOnly? I followed the steps to add CREATE_INFOPLIST_SECTION_IN_BINARY and I see the values being read correctly from Bundle… then this error throws up… I'm trying the startAccessingSecurityScopedResource that I read about… no luck. Hmm… AFAIK this is just a plain vanilla macOS command line tool (other than injecting an info.plist) with no funky sandbox behavior going on. Any ideas what else to try or where else to look?
Jan ’24
Reply to RandomAccessCollection Performance Problems when List is paired with NavigationStack
https://developer.apple.com/forums/thread/741484 https://github.com/apple/swift-collections/pull/335 was merged and these equality checks are now fast when comparing equal identities… I'm still not clear why SwiftUI would need dozens of equality checks to compare a let constant collection against itself… but I'm not sure there's anything left to do to optimize this from swift-collections for now.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23