I have a SwiftUI
–SwiftData
–CloudKit
app that has a TextField
. I would like to make it so that after a user types in text, it will update the TextField
on another device via CloudKit
. I have the following code:
struct CommentView: View
{
@Query private var comments: [Comment]
@State private var comment:Comment
@State private var text: String = ""
@State private var userInput = ""
private var filteredComment: Comment
{
if let comment = self.comments.first(where: { $0 == self.comment })
{
return comment
}
else
{
return self.comment
}
}
init(comment: Comment)
{
self.comment = comment
}
var body: some View
{
VStack
{
TextField("", text: self.$text)
.onAppear()
{
self.text = self.filteredComment.text
}
.onChange(of: self.text)
{
self.filteredComment.text = text
}
}
}
}
This code actually works fine and will update to another device via CloudKit
when a user exits out of the view and then reloads it. But I would like for it to update to the TextField
without reloading (i.e after the user finishes typing). I don’t understand why what I have above won’t work, I’m pulling the comment
object from the queried comments
array via a computed property. What am I missing here? Thanks.