处理200,000条以上的记录(4个属性),对其进行编辑将需要大约30倍的时间来插入新记录
我试图只保存结尾或更频繁地保存。 我在代码中采取的步骤。 1-创建MOC = newBackgroundContext()
2-所有工作都在此MOC中进行。执行{...}
3-查找具有相同ID的记录
4-使用MOC编辑内部旧记录。perform{...} // crash without it
5-或使用self.init(context:MOC)创建新记录
6-每10%的记录保存到MOC。
7-最后保存到MOC
let json = [String : [String:String]]()
let moc = CoreDataStack.sharedInstance().persistentContainer.newBackgroundContext()
moc.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
var notSavedCounter = 0
let threshHold = Int(0.1 * json.count)
moc.perform {
let now = Date()
for item in json {
notSavedCounter += 1
addAccess(context: moc, item: item)
//save records in intervels
if notSavedCounter > threshHold {
notSavedCounter = 0
do{
try moc.save()
}catch{
print("didnt save intervel : \(error.localizedDescription)")
}
}
//save at the end
if lastItem {
do{
try moc.save()
}catch{
print(" didnt save at end : \(error.localizedDescription)")
}
}
}
}
func addAccess(context: NSManagedObjectContext , item: (key: String, value: [String:String])) {
if let access = hasAccess(id: item.key){
access.modifyOldRecord(context: context, item: item)
}else{
_ = Access.init(context: context, item: item)
}
}
func hasAccess(id: String) -> Access? {
let request = NSFetchRequest<Access>(entityName: "Access")
request.predicate = NSPredicate(format: "id == %@", id)
request.fetchLimit = 1
return try! CoreDataStack.sharedInstance().persistentContainer.viewContext.fetch(request).first
}
extension Access {
convenience init (context: NSManagedObjectContext , item: (key: String, value: [String:String])){
self.init(context: context)
id = item.key
// assign attributes
}
func modifyOldRecord( context: NSManagedObjectContext , itemValue: [String:String]) {
context.perform {
// assign attributes
}
}
}
271500条记录的结果
插入所有记录30秒 编辑所有记录828.485 s〜14分钟