When I click the add button I get the message printed item save sucessfully. I have tested your code on macOS Sonoma release candidate (macOS 14.0 (23A339)) with Xcode Version 15.0 (15A240d)
Post
Replies
Boosts
Views
Activity
If possible also add the tag Swift, IMHO it might help to ask in swift.org (https://forums.swift.org/c/swift-users/15) and please cross link this post.
I feel even #Predicate<Entry>{$0.item == name} should work, give it a try without storing it in a variable.
Given below is my understanding (I could be wrong).
It looks like a concurrency issue.
@Observable does ensure properties are accessed on the main thread, so it is thread safe.
However by accessing a static property containing the AppModel, the AppModel might not longer guarantee thread safety.
Based on the crash it seems like it is caused by simultaneous access (possibly from different threads).
Debug
Turn on Swift Strict Concurrency to complete to spot any warnings thrown by the compiler.
Solution
If you want to pass around the model use @Environment across views and access them across views
If you have different models and need to talk to each other find ways to isolate and protect access using actors.
I have created a default SwiftData app by selecting the storage as SwiftData.
The app runs fine on iOS simulator, there is no crash.
I am using Xcode 15 Beta 6.
Do you have any framework that you have added to your project?
Can you clear DerivedData and create a new project and test using Xcode Beta 6?
Just curious:
Why do you need a shared static property on AppModel?
Since Folder conforms to identifiable in SidebarView could you just use ForEach(folders) { folder in?
Note: I don't have an iOS 17 device so couldn't test it on real device.
Could you make the above 2 changes mentioned and test it? Just trying to understand what causes the crash?
Based on my experience, using self inside #predicate is not allowed. Do you have unique identifier field for Item?
If so store that then use unique identifier in a variable and then use id inside your #predicate.
Assume name is the unique identifier then use the following code
example:
let id = self.name
let predicate = #predicate { $0.item.name == id }
....
Try removing didSet and try it
Try removing private set
One way to isolate the problem is to keep commenting out and simplifying your model till it no longer crashes and then you can isolate the problem.
Then file a feedback with the isolated problem.
Not sure this helps, on iOS 17 there is an API called scrollPosition which is an alternate to ScrollViewReader
Refer: https://developer.apple.com/wwdc23/10159
Any help or workaround would be much appreciated, I have filed a feedback FB12953838
Yeah I have trouble with FileRepresentation and Share, however I am able to drag and drop to the finder. Just check if you are able to drag and drop the finder (on macOS Sonoma). In my case it was a plain text file so I was using the contentType as utf8PlainText. When I had used .text it didn't work. Might be worth filing a feedback and posting the feedback ID.
Since @Model is a macro it would do something special (custom logic) for every member property to persist it.
Workaround
Create a struct ItemRecord with the same properties
Populate ItemRecord with Item
Conform ItemRecord to Codable and Transferable
Let the Item have a way to conveniently create ItemRecord
Use ItemRecord to drag and drop or for other transferable functionalities (item.record)
Code
@Model
final class Item {
let createdAt: Date
//For convenience
var record: ItemRecord {
ItemRecord(item: self)
}
init() {
self.createdAt = .now
}
}
struct ItemRecord: Codable, Transferable {
let createdAt: Date
init(item: Item) {
createdAt = item.createdAt
}
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .myCustomType)
}
}
@gchiste I have filed a feedback FB12980427 (contains sample code, videos)
I really hope that the bug gets fixed, I have filed it under SwiftUI as CoreTransferable wasn't available in the frameworks list.
Yes, this doesn't work on iOS but works on macOS.
I have tested on iOS 17 beta using Xcode 15 beta 6.
On Xcode 15 beta 6, following code compiles fine.
The following code was taken from the Code tab of the https://developer.apple.com/wwdc23/10154
Why do you explicitly mention PersistentModel conformance when @Model already conforms to PersistentModel?
Code (compiles fine)
@Model
final class Card {
var front: String
var back: String
var creationDate: Date
init(front: String, back: String, creationDate: Date = .now) {
self.front = front
self.back = back
self.creationDate = creationDate
}
}
Did you miss
Store the rawValue in a variable and access the variable inside the #Predicate closure.
Given below is an example:
enum AccountType: Int, Codable, CaseIterable {
case creditAccountReceivable = 1,
creditAccountPayable = 2,
nominal = 3
}
@Model
class Account {
var type: Int
init(type: AccountType.RawValue) {
self.type = type
}
}
struct ContentView: View {
@Query
private var nominalAccounts: [Account]
init() {
//Store it in a variable type
let type = AccountType.creditAccountPayable.rawValue
let filter = #Predicate<Account> { account in
//Access the variable inside the closure
account.type == type
}
_nominalAccounts = Query(filter: filter, sort: \.type)
}
var body: some View {
Text("Hello")
}
}
Just a small suggestion, next time please post minimum code that would compile.
Add the tag SwiftData to the post so that experts in SwiftData can view your question as your question is more related to SwiftData