问题描述
我有 2 个实体:组件和具有一对多关系的 SurveyReadingWeb.我已经在核心数据数据库中保存了组件数据;但是在保存 SurveyReadingWeb 时——在 for 循环中——我必须通过传递组件 id 并设置surveyreadings isComponentexists 关系来获取组件数据实体,就像下面的 8000 条记录的代码一样.这个过程太耗时了,将近7分钟.我怎样才能减少时间?
I have 2 entities: Components & SurveyReadingWeb that have a one-to-many relationship.I have saved Components data in core data database; but while saving SurveyReadingWeb -- in a for loop -- I have to fetch Components data entity by passing component id and setting the surveyreadings isComponentexists relationship, like in the code below for 8000 records. This process is taking too much time, nearly 7 minutes. How can I reduce the time?
for item in items {
autoIncrementId = autoIncrementId + 1
print("reading items parsed:- \(item.SurveyReadingId), \(autoIncrementId)")
let reading : SurveyReadingWeb? = NSEntityDescription.insertNewObject(forEntityName: Constants.EntityNames.kSURVEYREADINGWEB, into: self.managedContext) as? SurveyReadingWeb
reading?.surveyId1 = Int32(autoIncrementId)
reading?.surveyId = Int32(item.SurveyId) ?? 0
reading?.readingData = item.CH4
reading?.surveyDateTime = item.SurveyReadingDateTime
reading?.latitude = item.Latitude
reading?.longitude = item.Longitude
reading?.serialNumber = item.SerialNumber
reading?.descriptionText = item.Description
reading?.componentName = item.Description
reading?.componentId = Int32(item.ComponentId) ?? 0
reading?.readingTypeId = Int32(item.ReadingTypeID) ?? 0
reading?.readingTypeDetails = item.ReadingTypeDetails
reading?.currentLatitude = item.CurrentLatitude
reading?.currentLongitude = item.CurrentLongitude
reading?.hdop = item.HDOP
reading?.vdop = item.VDOP
reading?.componentDistance = item.ComponentDistance
reading?.facilityId = Int32(item.ProjectId) ?? 0
reading?.posted = 1
reading?.readyToSync = 1
reading?.isComponentExists = DatabaseManager.sharedManager.getCompNameFromID(compID: Int32(item.ComponentId) ?? 0)
}
saveContext()
func getCompNameFromID(compID : Int32) -> Components? {
var component : Components? = nil
let fetchRequest : NSFetchRequest<Components> = Components.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "componentId == %ld", compID)
do {
let searchResults = try managedContext.fetch(fetchRequest)
component = (searchResults.first) ?? nil
} catch {
}
return component ?? nil
}
推荐答案
如果你添加一个 NSCache
实例来缓存 compID:Components
键值,你可以显着减少时间对.
You can reduce the time dramatically if you add an NSCache
instance to cache compID:Components
key value pairs.
在 getCompNameFromID
中检查 compID
是否存在于缓存中.如果是从缓存中获取值(非常快),如果没有获取记录并将其保存在缓存中的 compID
中.
In getCompNameFromID
check if the compID
exists in the cache. If yes get the value from the cache (very fast), if not fetch the record and save it for the compID
in the cache.
并使用方便的 SurveyReadingWeb(context:
初始化程序来摆脱所有丑陋的问号,但性能优势却很小.
And use the convenient SurveyReadingWeb(context:
initializer to get rid of all the ugly question marks, however the performance benefit is rather tiny.
这篇关于核心数据需要时间来插入带有获取实体的记录 &设为关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!