Check data exist before saving in coredata -Swift

前端 未结 1 1573
南旧
南旧 2021-01-26 02:38

While saving the data am checking whether the data is already exist or not in the entity. Problem here is the above method is not working as expected, when I run the app for ver

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 02:43

    First you create a User object in your managed context before calling someEntityExists so most likely this object will be included in the fetch even if it hasn't been saved yet.

    Call someEntityExists before even creating a new user entity to see if you should proceed or not

    func saveUserInfo(detail:[String:Any])-> Bool {
        if someEntityExists(id: detail["email"] , entityName: "User", type: "String", fieldName: "email") {
            print("Data is already present")
             return false
        }
        //else create and save new user
    }
    

    0 讨论(0)
提交回复
热议问题