title: logically false fetch request
body: <p>I am doing a fetch request with a predicate mentioned in the block quote below, but I seem to get "logically false fetch request". What does this message mean and what step should I take to find the problem and resolve it?</p>
  
  <blockquote>
    <p>annotation: logically false fetch request  (entity: Country; predicate: ("alpha3Code" == "KOR"); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) short circuits.</p>
  </blockquote>
  
  <p>Here is the code that I get the error from</p>
  
  <pre><code>let record = Currency.fetch(id: currency)[0] as! Currency
  </code></pre>
  
  <p>where Currency class is as follows. "fetch" is implemented in NSManagedObjectProtocol trait</p>
  
  <pre><code>public class Currency: NSManagedObject, NSManagedObjectProtocol, XMLImporterDelegate {
  
      private static let attributes = ["name", "currency", "decimalUnit", "isUsed"]
      private static let xmlRecordTag = "CcyNtry"
      private static let xmlAttributeTags = ["CcyNm": attributes[0],
                                             "Ccy": attributes[1],
                                             "CcyMnrUnts": attributes[2]]
  
      static func setValue(managedObject: NSManagedObjectProtocol, object: Dictionary&lt;String, Any&gt;) {
          let currency = managedObject as! Currency
          currency.name = getString(from: object, withKeyValue: attributes[0])
          currency.currency = getString(from: object, withKeyValue: attributes[1])
          currency.decimalUnit = getInt16(from: object, withKeyValue: attributes[2])
          currency.isUsed = getBool(from: object, withKeyValue: attributes[3])
          return
      }
  
      static func getPredicates(forID id: Dictionary&lt;String, Any&gt;) -&gt; [NSPredicate] {
          var predicates: [NSPredicate] = []
          predicates.append(NSPredicate.init(format: "%@ = %@", attributes[1], getString(from: id, withKeyValue: attributes[1])))
          return predicates
      }
  
      func isEqual(object: NSManagedObjectProtocol) -&gt; Bool {
          if let object = object as? Currency {
              if object.currency == self.currency { return false }
              return true
          } else {
              return false
          }
      }
  
      static func recordTag() -&gt; String {
          return xmlRecordTag
      }
  
      static func attribute(byTag tag: String) -&gt; String? {
          return xmlAttributeTags[tag]
      }
  
      static func getUsed() -&gt; [Any]?{
          var predicates: [NSPredicate] = []
          predicates.append(NSPredicate.init(format: "%@ = %@", attributes[3], NSNumber(booleanLiteral: false)))
          return fetch(predicates: predicates)
      }
  }
  </code></pre>
  
  <p>NSManagedObjectProtocol has following trait</p>
  
  <pre><code>extension NSManagedObjectProtocol {
      public static func add(from objectValue: Dictionary&lt;String, Any&gt;) -&gt; NSManagedObjectProtocol? {
          let exists = fetch(id: objectValue)
          if exists.count &gt; 0 {
              NSLog("Object already exists in CoreData : %@", objectValue.description)
              return nil
          } else {
              return newObject(object: objectValue)
          }
      }
  
      public static func addOrChange(from object: Dictionary&lt;String, Any&gt;) -&gt; NSManagedObjectProtocol {
          let exists = fetch(id: object)
          if exists.count &gt; 0 {
              // TODO: confirm if data needs to be changed rather than delete and insert
          }
          delete(id: object)
          return add(from: object)!
      }
  
      public static func getString(from object: Dictionary&lt;String, Any&gt;, withKeyValue key: String) -&gt; String {
          return object[key] as! String
      }
  
      public static func getInt16(from object: Dictionary&lt;String, Any&gt;, withKeyValue key: String) -&gt; Int16 {
          if let stringValue = object[key] as? String {
              if let intValue = Int(stringValue) {
                  return Int16(intValue)
              } else {
                  return 0
              }
          } else if let intValue = object[key] as? Int {
              return Int16(intValue)
          } else {
              return 0
          }
      }
  
      public static func getBool(from object: Dictionary&lt;String, Any&gt;, withKeyValue key: String) -&gt; Bool {
          if let boolValue = object[key] as? Bool {
              return boolValue
          } else {
              return false
          }
      }
  
      public static func fetch(predicates: [NSPredicate] = [], sortDescriptors: [NSSortDescriptor] = []) -&gt; [Any] {
          let request = Self.request(predicates: predicates)
          do {
              return try CoreDataHelper.getCoreDataHelper().context.fetch(request)
          } catch {
              return []
          }
      }
  
      public static func fetch(id: Dictionary&lt;String, Any&gt;) -&gt; [Any] {
          return Self.fetch(predicates: Self.getPredicates(forID: id))
      }
  
      public static func delete(predicates: [NSPredicate] = []) {
          let context = CoreDataHelper.getContext()
          let fetchRequest = request(predicates: predicates)
          let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
          do {
              try context.execute(deleteRequest)
              CoreDataHelper.getCoreDataHelper().saveContext()
          } catch {
              NSLog("Delete request failed")
              return
          }
      }
  
      public static func delete(id: Dictionary&lt;String, Any&gt;) {
          delete(predicates: getPredicates(forID: id))
      }
  
      // MARK: - Private API
      private static func newObject(object: Dictionary&lt;String, Any&gt;) -&gt; NSManagedObjectProtocol {
          let entityName = String(describing: self)
          let context = CoreDataHelper.getContext()
          let managedObject = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context) as! NSManagedObjectProtocol
          setValue(managedObject: managedObject, object: object)
          CoreDataHelper.getCoreDataHelper().saveContext()
          return managedObject
      }
  
      private static func request(predicates: [NSPredicate] = [], sortDescriptors: [NSSortDescriptor] = []) -&gt; NSFetchRequest&lt;NSFetchRequestResult&gt; {
          // Prepare a request
          let entityName = String(describing: self)
          let classObject: AnyClass! = NSClassFromString(entityName)
          let objectType: NSManagedObject.Type = classObject as! NSManagedObject.Type!
          let request: NSFetchRequest&lt;NSFetchRequestResult&gt; = objectType.fetchRequest()
  
          // Add predicates
          if predicates.count &gt; 0 {
              request.predicate = NSCompoundPredicate.init(andPredicateWithSubpredicates: predicates)
          }
  
          // Add sortDescriptors
          if sortDescriptors.count &gt; 0 {
              request.sortDescriptors = sortDescriptors
          }
  
          return request
      }
  }
  </code></pre>
  
  <p>Finally this is how the CoreDataHelper looks like.</p>
  
  <pre><code>class CoreDataHelper: NSObject {
      var context: NSManagedObjectContext!
      var model: NSManagedObjectModel!
      var coordinator: NSPersistentStoreCoordinator!
      var store: NSPersistentStore!
      let storeFilename = "Accounting.sqlite"
  
      func setupCoreData() {
          self.loadStore()
      }
  
      func saveContext() {
          if (self.context.hasChanges) {
              do {
                  try context.save()
              } catch {
  
              }
          }
      }
  
      func applicationDocumentDictionary() -&gt; String {
          let directory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
          NSLog("SQLite Directory : %@", directory)
          return directory
      }
  
      func applicationStoresDirectory() -&gt; URL {
          let storesDirectory = URL.init(fileURLWithPath: self.applicationDocumentDictionary()).appendingPathComponent("Stores")
          let fileManager = FileManager.default
          if (!fileManager.fileExists(atPath: storesDirectory.path)) {
              do {
                  try fileManager.createDirectory(at: storesDirectory,
                                                  withIntermediateDirectories: true,
                                                  attributes: nil)
              } catch {
  
              }
          }
          return storesDirectory
      }
  
      func storesURL() -&gt; URL {
          return self.applicationStoresDirectory().appendingPathComponent(storeFilename)
      }
  
      override init() {
          super.init()
          model = NSManagedObjectModel.mergedModel(from: nil)
          coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
          context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
          context.persistentStoreCoordinator = coordinator
      }
  
      func loadStore() {
          if (store != nil) {
              return
          } else {
              do {
                  try store = coordinator.addPersistentStore(ofType: NSSQLiteStoreType,
                                                             configurationName: nil,
                                                             at: self.storesURL(),
                                                             options: nil)
              } catch {
  
              }
          }
      }
  
      static func getCoreDataHelper() -&gt; CoreDataHelper {
          let appDelegate = UIApplication.shared.delegate as! AppDelegate
          return appDelegate.coreDataHelper
      }
  
      static func getContext() -&gt; NSManagedObjectContext {
          return getCoreDataHelper().context
      }
  }
  </code></pre>
  
  <p>Just to let you know that Country class has alpha3Code.</p>
  
  <pre><code>extension Country {
  
      @nonobjc public class func fetchRequest() -&gt; NSFetchRequest&lt;Country&gt; {
          return NSFetchRequest&lt;Country&gt;(entityName: "Country");
      }
  
      @NSManaged public var englishName: String?
      @NSManaged public var frenchName: String?
      @NSManaged public var alpha2Code: String?
      @NSManaged public var alpha3Code: String?
      @NSManaged public var countryNumber: Int16
  
  }
  </code></pre>