0

I am able to save and retrieve data from Core Data, but I don't know how to not duplicate or avoid the same saved data in Core Data. How should I do this? Any help would be really thankful.

Saving data:

let historyData = trackHistoryData()
historyData.HistorygetConsignmentNo = self.getTrackno
print("cnno",historyData.HistorygetConsignmentNo)
historyData.HistorydateStr = jsonArray[0]["date"] as! String
print("date",historyData.HistorydateStr)
historyData.HistoryofficeStr = jsonArray[0]["office"] as! String
print("office",historyData.HistoryofficeStr)
historyData.HistoryprocessStr = jsonArray[0]["process"] as! String
print("process", historyData.HistoryprocessStr)

let history = HistoryTrack(context: persistenceService.context)

history.cnNo = historyData.HistorygetConsignmentNo
history.date = historyData.HistorydateStr
history.office = historyData.HistoryofficeStr
history.process = historyData.HistoryprocessStr
persistenceService.saveContext()

Retrieving Data:

let fetchRequest:NSFetchRequest<HistoryTrack> = HistoryTrack.fetchRequest()

do {
    let historytrack = try persistenceService.context.fetch(fetchRequest)
    self.trackhistoryData = historytrack
    self.trackHistoryTblView.reloadData()
} catch {}
Renzo Tissoni
  • 642
  • 9
  • 21
Vignesh Krish
  • 21
  • 1
  • 7
  • Are you trying to update a CoreData entry and accidentally duplicating it? – Daniel Jul 15 '19 at 16:11
  • @Daniel I am aware that i am duplicating the same value but i don't want to save the same value in core data if it already exist. I don't know what code to check for not duplicating it while i save it? Eg. in the above method cnno no is unique string and i can check with that if it exist already(if so it shouldn't copy again). But how to write code to check ? – Vignesh Krish Jul 15 '19 at 17:46
  • https://stackoverflow.com/questions/21130427/how-to-add-unique-constraints-for-some-fields-in-core-data – Gurjinder Singh Jul 16 '19 at 10:06

0 Answers0