如何不在核心数据中再次保存已经保存的数据?

时间:2019-07-15 13:57:21

标签: ios swift core-data

我能够从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 {}

0 个答案:

没有答案