title: SwiftUI cannot update CoreData Entity Object
body: <p>I am facing a very weird bug, I have a edit function which will accept the object and new form data from the frontend, the function can be executed without errors but the issue is the data is not updated at all. I tried reopen the simulator and refetch also no use to this. May I ask how to solve this?</p>
  <pre><code>class RoomDataController: ObservableObject {
      @Published var rooms: [Room] = []
  
      let container = NSPersistentContainer(name: &quot;RentalAppContainer&quot;)
      
      init() {
          container.loadPersistentStores { description, error in
                  let current = FileManager.default.currentDirectoryPath
                  print(current)
  
              if let error = error {
                  print(&quot;Failed to load data in DataController \(error.localizedDescription)&quot;)
              }
          }
          fetchRooms()
      }
  
      func save(context: NSManagedObjectContext) {
          do {
              try context.save()
              print(&quot;Data created or updated successfully!&quot;)
          } catch {
              // Handle errors in our database
              let nsError = error as NSError
              fatalError(&quot;Unresolved error \(nsError), \(nsError.userInfo)&quot;)
          }
      }
  
      func editRoom(room: Room, roomType: String, roomDescription: String, contactPeriod: String, roomPrice: Float, userID: UUID, isShort: Bool, isLong: Bool, images: String, email: String, phoneNum:String, context: NSManagedObjectContext) {
              
              room.roomType = roomType
              room.roomPrice = roomPrice
              room.roomDescription = roomDescription
              room.contactPeriod = contactPeriod
              room.contactEmail = email
              room.contactNum = phoneNum
              room.updated_at = Date()
              print(&quot;room to be edited: &quot;, room)
              save(context: container.viewContext)
              fetchRooms()
          }
  }
  </code></pre>
  <p>The Frontend View that call the edit function</p>
  <pre><code>struct EditRoomView: View {
      @EnvironmentObject var userAuth: UserAuth
  
      @State var type: String = &quot;&quot;
      @State var description: String = &quot;&quot;
      @State var price: String = &quot;&quot;
      @State var contactPeriod: String = &quot;&quot;
      @State var isShort: Bool = false
      @State var isLong: Bool = false
      @State var email: String = &quot;&quot;
      @State var phoneNum: String = &quot;&quot;
  
      @ObservedObject var room: Room
      private func editItem() {
          RoomDataController().editRoom(
              room: room,
              roomType: self.type,
              roomDescription: self.description,
              contactPeriod: self.contactPeriod,
              roomPrice: Float(self.price)!,
              userID: userAuth.user.id!,
              isShort: self.isShort,
              isLong: self.isLong,
              images: &quot;room-1,room-2,room-3&quot;,
              email: email,
              phoneNum: phoneNum
          )
      }
      
      init(room: Room){
          self.room = room
          _type = State(initialValue: room.roomType!)
          _description = State(initialValue: room.roomDescription!)
          _price = State(initialValue: String( room.roomPrice))
          _contactPeriod = State(initialValue: room.contactPeriod!)
          _email = State(initialValue: room.contactEmail!)
          _phoneNum = State(initialValue: room.contactNum!)
          if room.contactPeriod == &quot;Short&quot;{
              _isShort = State(initialValue:true)
              _isLong = State(initialValue: false)
          } else {
              _isShort = State(initialValue: false)
              _isLong = State(initialValue:true)
          }
      }
      
      
      
      var body: some View {
          VStack{
              HStack {
                  Spacer()
                  VStack {
                      Menu {
                          Button {
  //                            self.selectedTab = .AccountViewTab
                          } label: {
                              NavigationLink(destination: AccountView().environmentObject(userAuth), label: {Text(&quot;My Profile&quot;)})
                              Image(systemName: &quot;arrow.down.right.circle&quot;)
                          }
                          Button {
                              userAuth.isLoggedin = false
                          } label: {
                              Text(&quot;Log Out&quot;)
                              Image(systemName: &quot;arrow.up.and.down.circle&quot;)
                          }
                      } label: {
                          Text(&quot;Menu&quot;).foregroundColor(Color.black)
                      }
                  }
                  
                  Spacer()
                  VStack {
                      NavigationLink(destination: OwnerNavbarView().environmentObject(userAuth), label: {Text(&quot;Room Rent&quot;).foregroundColor(Color.black)})
                  }
                  .onTapGesture {
                  }
                  Spacer()
                  VStack {
                      NavigationLink(destination: OwnerNavbarView(nextView: &quot;PostRoomHistoryViewTab&quot;).environmentObject(userAuth), label: {Text(&quot;Post History&quot;).foregroundColor(Color.black)})
                  }
                  .onTapGesture {
                  }
                  Spacer()
                  VStack {
                      NavigationLink(destination: OwnerNavbarView(nextView: &quot;CustomerOrderViewTab&quot;).environmentObject(userAuth), label: {Text(&quot;Customer Orders&quot;).foregroundColor(Color.black)})
                  }
                  .onTapGesture {
                  }
                  Spacer()
              }
              .padding(.bottom)
              .background(Color.gray.edgesIgnoringSafeArea(.all))
              
              Text(&quot;Edit Room Details&quot;)
              Form {
                  Section(header: Text(&quot;Room Type&quot;) ) {
                      TextField(&quot;&quot;, text: self.$type)
                  }
                  
                  Section(header: Text(&quot;Room Description&quot;) ) {
                      TextField(&quot;&quot;, text: self.$description)
                  }
                  
                  MultipleImagePickerView()
                  
                  Section(header: Text(&quot;Room Price&quot;) ) {
                      TextField(&quot;&quot;, text: self.$price)
                  }
                  Section(header: Text(&quot;Contact Period&quot;) ) {
                      
                      HStack{
                          Toggle( &quot;Short&quot;, isOn: $isShort )
                          
                          Toggle(&quot;Long&quot;, isOn: $isLong )
                          
                      }
                  }
                  
                  Section(header: Text(&quot;Contact Email Address * :&quot;) ) {
                      TextField(&quot;&quot;, text: self.$email)
                  }
                  Section(header: Text(&quot;Contact Mobile No * :&quot;) ) {
                      TextField(&quot;&quot;, text: self.$phoneNum)
                  }
                  
              }
  
              Button(action: {
                  self.editItem()
              }) {
                  HStack {
                      Text(&quot;Update&quot;)
                  }
                  .padding()
                  .scaledToFit()
                  .foregroundColor(Color.white)
                  .background(Color.blue)
                  .cornerRadius(5)
              }
          }
      }
  }
  </code></pre>
  <p>Ok..Managed to solve this in this way, buy why?</p>
  <pre><code>do {
      fetchRequest.predicate = NSPredicate(format: &quot;id == %@&quot;, room.id?.uuidString as! CVarArg)
      let result = try container.viewContext.fetch(fetchRequest)
      result[0].roomType = roomType
      result[0].roomPrice = roomPrice
      result[0].roomDescription = roomDescription
      result[0].contactPeriod = contactPeriod
      result[0].contactEmail = email
      result[0].contactNum = phoneNum
      result[0].updated_at = Date()
  } catch {
      print (&quot;fetch task failed&quot;, error)
  }
  if room.ownerID == userID.uuidString {
      save(context: container.viewContext)
      fetchRooms()
  }
  </code></pre>