'Swift СoreData Predicate returns without one record

Such a problem that in one screen my getCount function returns for example 5 records

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
      return
    }
    var managedContext = appDelegate.persistentContainer.viewContext
    
      managedContext = appDelegate.persistentContainer.viewContext
     print(getCount(viewContext2:  managedContext)) // 5


func getCount(viewContext2:  NSManagedObjectContext)-> Int{

let fetchRequest: NSFetchRequest<Cart> = Cart.fetchRequest()

fetchRequest.predicate = NSPredicate(format: "real_id == %@","2086" )

do {
let resCart1ById2 =  try  viewContext2.fetch(fetchRequest)
     
    return resCart1ById2.count //   5 
} catch let error as NSError {
    print(error)
}
return -1

}

When switching to another child screen, the same function returns 4, does not see one record How can the problem be solved?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source