核心数据条件

时间:2017-03-18 10:30:35

标签: swift core-data

我需要添加核心数据条件我该怎么做? 。如果记录处于这种情况,“sonuc”变量将为真

核心数据模型:

enter image description here

  

如果“kullaniciadi”是“emre”而“otogiris”是“1”那么“sonuc”将   是真的

func getContext () -> NSManagedObjectContext {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        if #available(iOS 10.0, *) {
            return appDelegate.persistentContainer.viewContext
        } else {
            return DatabaseController.managedObjectContext
        }
    }



func otomatikGirisKontrol() -> Bool
    {
        var sonuc = false
        let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Kullanicilar")
        fetchRequest.returnsObjectsAsFaults = false
        do{
            let results = try getContext().fetch(fetchRequest)
            if results.count > 0
            {
            sonuc = true
            }
            else
            {
            sonuc = false
            }

        }
       catch
          {

          }
        return (sonuc)
    }

1 个答案:

答案 0 :(得分:0)

使用NSPredicate仅提取适合您条件的Kullaniciar个对象:

let fetchRequest = ...

let kullaniciadi = "emre"
let otogiris = "1"
let predicate = NSPredicate(format: "kullaniciadi = %@ AND otogiris = %@", kullaniciadi, otogiris)

request.predicate = predicate

//... execute fetch request