我能够从Core Data中保存和检索数据,但是我不知道如何不重复或避免在Core 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()
检索数据:
let fetchRequest:NSFetchRequest<HistoryTrack> = HistoryTrack.fetchRequest()
do {
let historytrack = try persistenceService.context.fetch(fetchRequest)
self.trackhistoryData = historytrack
self.trackHistoryTblView.reloadData()
} catch {}