Post

Replies

Boosts

Views

Activity

Reply to Memory usage unstoppably increasing when updating SwiftData Object from Timer
So apparently something is wrong in this function. A timer makes the problem more obvious but seems to not have a direct effect. I want to change a property of a swiftdata object without using @query. In this case insert does not add a new item but updates the existing one, exactly the behavior I am looking for. func doManually() { let descriptor = FetchDescriptor<Item>() let results = (try? Helper.shared.modelContext?.fetch(FetchDescriptor<Item>())) ?? [] let element = results.first! element.timestamp = Date.now Helper.shared.modelContext?.insert(element) }
May ’24
Reply to SwiftData @Model does not conform to the 'Sendable' protocol; thi
A SwiftData Model cannot cross actor boundaries to avoid data races. You shouldn't make your model conform to Sendable because it still doesn't. I am currently in the same situation and as far as I can tell the best option to migrate to Swift 6 when using SwiftData is using ModelActor There are not as many tutorials and articles about it as other SwiftData topics but I believe this is the way to go. Do not return SwiftData Models from your ModelActor. This may result in data races when these models are accessed from different threads. Only return sole properties and the persistentModelIDto identify models. This is what my actor looks like: @ModelActor actor DataHandler { public func fetch<T>(_ descriptor: FetchDescriptor<T>) throws -> [PersistentIdentifier] { return try self.modelContext.fetchIdentifiers(descriptor) } public func update<T>(_ persistentIdentifier: PersistentIdentifier, keypath: ReferenceWritableKeyPath<YourModel, T>, to value: T) throws { guard let model = modelContext.model(for: persistentIdentifier) as? YourModel else { // Error handling } model[keyPath: keypath] = value } public func read<T>(_ persistentIdentifier: PersistentIdentifier, keypath: ReferenceWritableKeyPath<YourModel, T>) throws -> T { guard let result = modelContext.model(for: persistentIdentifier) as? Hoerspiel else { // Error Handling } return result[keyPath: keypath] } // And others like delete fetchCount etc. I think you get the point } You can also return a struct that has the same properties as your model if you need to read properties in a synchronous context. This struct will automatically conform to Sendable if you only use standard data types like Ints and Bools etc. Otherwise make it conform to Sendable. You will probably also have the error Passing argument of non-sendable type FetchDescriptor outside of outside main actor-isolated context may introduce data races You can either just never modify the fetch descriptor inside the modelActor (which there is no reason to do anyway) and mark the FetchDescriptor @unchecked Senable. I think the better option is to define your fetchDescriptor, modify it to your needs and then create a let constant that copies the previous fetchDescriptor. This way it cannot be mutated and therefore no data races can occur. I hope you were able to follow. Happy to provide clarifications or corrections if someone has found a better option.
Jul ’24
Reply to App icon
You have to do the rounding of the corners yourself if you are building an app for macOS. (Except Mac Catalyst iirc) Your entire app icon document should have a size of 1024 x 1024. The icon itself should be 824 x 824 with a corner radius of 185.4 so it looks like you desire. Source: https://forums.developer.apple.com/forums/thread/670578?answerId=739409022#739409022
Aug ’24
Reply to SwiftData @Model does not conform to the 'Sendable' protocol; thi
@SpaceMan You should use this instead: public func fetch<T>(_ descriptor: @Sendable () -> FetchDescriptor<T>) throws -> [PersistentIdentifier] { return try self.modelContext.fetchIdentifiers(descriptor()) } and you use that like: guard let loaded = try? await dataHandler.handler.fetch( { FetchDescriptor<MyModel>(predicate: #Predicate { model in model.somebool }, sortBy: [SortDescriptor(\.someKeypath)]) }) else { return }
Aug ’24
Reply to SwiftData serious bug with relationships and CloudKit in iOS 18.0 (Xcode 16 Beta)
Just a quick idea: Can you access that relationship programmatically? So if you add a button that tries to print that relationship, does that work? Currently there is a bug that @Query does not update the view after an iCloud Sync. I filed a bug report for that. FB14619787 If you can indeed access that relationship you might be able to work around that by using this package (https://github.com/ggruen/CloudKitSyncMonitor) that can update your view whenever the iCloud sync status changes.
Sep ’24
Reply to MusicKit: How to search for a single song by ID
You need to infer a type manually and tell which property should match yours. MusicCatalogResourceRequest<Song>(matching: \.id, equalTo: song.id.rawValue) This is what you are looking for. Apart from that I strongly advise you to switch using IRSC (or UPC for albums) instead of the song id. The song id (or album id) can change at any time. Read the explanation here
Sep ’24