Title: FetchedResults won't trigger and SwiftUI update but the context saves it successfully
Tags: ios;iphone;core-data;swiftui
Question: I am trying to update an attribute in my Core Data through an NSManagedObject. As soon as I update it, I save the context and it gets saved successfully.
Problem
After the context saves it, the UI (SwiftUI) won't update it with the new value. If I add a completely new Children into Core Data (insert) the UI gets updated.
What I tried:
Asperi approach - I can print out the correct new value in .onReceive but the UI doesn't update
Using self.objectWillChange.send() before context.save() - didn't work either
Changed the Int16 to String, because I was speculating that somehow Int16 is not observable? Didn't work either
Is this a SwiftUI bug?
As-is State
//only updating the data
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let fetchReq = NSFetchRequest<Card&gt;(entityName: "Card") //filter distributorID; should return only one Card
fetchReq.resultType = .managedObjectResultType
fetchReq.predicate = NSPredicate(format: "id == %@", params["id"]!) //I get an array with cards
var cards :[Card] = []
cards = try context.fetch(fetchReq)
//some other functions
cards[0].counter = "3" //
self.objectWillChange.send() //doesn't do anything?
try context.save()
//sucessfully, no error. Data is there if I restart the app and "force" a reload of the UI
//Core Data "Card"
extension Card :Identifiable{
@nonobjc public class func fetchRequest() -&gt; NSFetchRequest<Card&gt; {
return NSFetchRequest<Card&gt;(entityName: "Card")
//...
@NSManaged public var id: String
//....
//Main SwiftUI - data is correctly displayed on the card
@FetchRequest(entity: Card.entity(),
sortDescriptors: [],
predicate: nil)
var cards: FetchedResults<Card&gt;
List {
ForEach(cards){  card in
CardView(value: Int(card.counter)!, maximum: Int(card.maxValue)!,
headline: card.desc, mstatement: card.id)

Here is the accepted answer: If first code block is a part of ObservableObject, then it does not look that third block, view one, depends on it, and if there is no changes in dependencies, view is not updated.
Try this approach.
But if there are dependencies, which are just not provided then change order of save and publisher as
try context.save()
self.objectWillChange.send()
Comment for this answer: I have setup an EnvironmentObject which is stuck on the main view. In there I am changing the data and saving the context. But context ist just         let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
Comment for this answer: Got it working in this order - now the UI updates: 1. save the context 2. reset the context 3. self.objectWillChange.send()


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/16/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><iphone><core-data><swiftui>', 'Title': "FetchedResults won't trigger and SwiftUI update but the context saves it successfully", 'ViewCount': 785, 'id': 61843543, 'user_AboutMe': '<p>Ibrahim.</p>\n', 'user_AccountId': 269453.0, 'user_DisplayName': 'cocos2dbeginner', 'user_Reputation': 2155.0, 'user_Views': 411.0, 'user_id': 558801.0}}