I have store data of my contacts in core data entity, and i'm trying to get that data in an array of that entity and populate data in my table view, now when i get that data i got duplicate data . How can delete my duplicate data from the array of my core data entity. This is the code how am i getting data from entity,
func getAllContacts()
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Contacts")
request.returnsObjectsAsFaults = false
do {
let result = try context.fetch(request)
allContacts = result as! [Contacts]
for contact in allContacts
{
let register = contact.register
if register == "yes"
{
self.contactsCoreDataRavenNameArray.append(contact.name!)
self.contactsCoreDataRavenNumberArray.append(contact.number!)
self.contactsCoreDataRavenImageArray.append(contact.image!)
}
else {
self.contactsCoreDataNonRavenNameArray.append(contact.name!)
self.contactsCoreDataNonRavenNumberArray.append(contact.number!)
self.contactsCoreDataNonRavenImageArray.append(contact.image!)
}
contactsCoreDataNameServer.append(contact.name!)
contactsCoreDataNumberServer.append(contact.number!)
contactsCoreDataImageServer.append(contact.image!)
}
} catch {
print("Failed")
}
}
This is how i'm storing api response data in my core data entity,
func compareWithRavenServerContacts(_ refreshing: Bool) {
if let token = LoginToken {
let param = [
"token" : token,
"contacts" : numberArrayForServer,
"names" : namesArrayForServer
] as [String : Any]
print(param)
if refreshing {
DispatchQueue.main.async {
topViewController()?.view.makeToastActivity(.center)
}
}
ServerCall.makeCallWitoutFile(GetRavenContactsUrl, params: param, type: Method.POST, currentView: nil) { (response) in
if let json = response {
print(json)
if json["success"].boolValue {
let data = json["data"].arrayValue
print(data.count,data)
var ravenContacts = [ContactStruct]()
var serverContacts = [ContactStruct]()
var NonRavenContacts = [ContactStruct]()
for item in data
{
let name = item["name"].stringValue
let phone = item["phone"].stringValue
let picture = item["picture"].stringValue
let register = item["is_register"].stringValue
let optData = try? self.updateContact(name: name, number: phone)
guard let data1 = optData else {
return
}
print(data1)
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let context = appDelegate?.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Contacts", in: context!)
let user = NSManagedObject(entity: entity!, insertInto: context)
user.setValue(name, forKey: "name")
user.setValue(phone, forKey: "number")
user.setValue(register, forKey: "register")
user.setValue(picture, forKey: "image")
do {
try context?.save()
print("Saved successfully.")
} catch {
print("Fail to save")
}
let contact = ContactStruct(name: name, number: phone, register: register, profilePic: picture, isSelected: false)
if register == "yes" {
ravenContacts.append(contact)
} else {
NonRavenContacts.append(contact)
}
serverContacts.append(contact)
}
self.contactsRaven = ravenContacts.sorted(by: { ($0.name < $1.name) })
self.contactsServer = serverContacts.sorted(by: { ($0.name < $1.name) })
self.contactsNonRaven = NonRavenContacts.sorted(by: { ($0.name < $1.name) })
self.getAllContacts()
self.contactsDevice = self.contactsServer
topViewController()?.view.hideToastActivity()
}
else
{
topViewController()?.view.makeToast("Error!. Please try again", duration: 2.0, position: .center)
topViewController()?.view.hideToastActivity()
}
}
}
}
else {
DispatchQueue.main.async {
topViewController()?.view.makeToast("Error!, Login Again To Call Services", duration: 2.0, position: .center)
topViewController()?.view.hideToastActivity()
}
}
}