Handling duplicate entries in Core Data

前端 未结 6 493
野的像风
野的像风 2021-02-04 10:28

I have an app that allows users to save favorites. I am using Core Data to store the favorites as managed objects. I have written some code to prevent the possibility of storing

6条回答
  •  独厮守ぢ
    2021-02-04 10:39

    Swift 3:

    func isExist(id: Int) -> Bool {
        let fetchRequest = NSFetchRequest(entityName: myEntityName)
        fetchRequest.predicate = NSPredicate(format: "id = %d", argumentArray: id)
    
        let res = try! theContext.fetch(fetchRequest)
        return res.count > 0 ? true : false
    }
    

提交回复
热议问题