我有类似的方法:
transformDexWithInstantRunSlicesApkForAwsDebug
达到这一点时,我的应用程序崩溃了:
如果让DeletedObjectIDs = batchDeleteResult.result设为? [NSManagedObjectID] { NSManagedObjectContext.mergeChanges(fromRemoteContextSave:[NSDeletedObjectsKey:DeletedObjectIDs],放入:[Context]) }
我在数组中拥有的ID如下:
deleteSectorIds的打印说明: ▿24个元素 -0:“ 8” -1:“ 9” -2:“ 11” -3:“ 12” -4:“ 13” -5:“ 14” -6:“ 15” -7:“ 16” -8:“ 17” -9:“ 18” -10:“ 19” -11:“ 20” -12:“ 21” -13:“ 22” -14:“ 23” -15:“ 24” -16:“ 25” -17:“ 27” -18:“ 28” -19:“ 29” -20:“ 30” -21:“ 31” -22:“ 32” -23:“ 33”
而且我似乎无法弄清楚为什么会出现此错误:
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“在实体中找不到关键路径ID”
当我在数组中显然没有ID值为7的时候?
任何人都可以为我分解到底是怎么回事?
我的模型如下:
导入基金会 导入ObjectMapper
枚举SectorsModelEnum:字符串{
func syncSectors(sectors: [SectorsModel]){
BackgroundContext.performAndWait {
//Fetch results from the database
let matchingSectorRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Sectors")
//Delete Sector ids
let deleteSectorIds = sectors.map { $0.ID }.compactMap { $0 }
matchingSectorRequest.predicate = NSPredicate(format: " !(ANY sector_id IN %@) AND (sector_id != nil)", argumentArray: [deleteSectorIds])
let batchDeleteRequest = NSBatchDeleteRequest.init(fetchRequest: matchingSectorRequest)
batchDeleteRequest.resultType = .resultTypeObjectIDs
// Execute the request to do batch delete and merge the changes to viewContext, which triggers the UI update
do {
let batchDeleteResult: NSBatchDeleteResult = try BackgroundContext.execute(batchDeleteRequest) as! NSBatchDeleteResult
if let deletedObjectIDs = batchDeleteResult.result as? [NSManagedObjectID] {
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: [NSDeletedObjectsKey: deletedObjectIDs], into: [Context])
}
} catch {
print("Error: \(error)\nCould not batch delete existing records.")
return
}
//******************** End of Delete Sectors *****************************
// Insert new SubSectors
for sector in sectors where !sector.exists {
_ = saveSectors(sectors: sectors)
}
do {
// Save all changes on background context
try BackgroundContext.save()
Context.performAndWait {
do {
// Saves the data from the background to the main context
try Context.save()
} catch {
print("Failure to save context: \(error)")
}
}
} catch {
print("Error: \(error)\nCould not save new records.")
BackgroundContext.rollback()
return
}
//******************** End of Insert new Sectors *****************************
//Update Sectors
for sector in sectors where !sector.ID.isNilOrEmpty{
let batchUpdateRequest = NSBatchUpdateRequest.init(entityName: "Sectors")
batchUpdateRequest.resultType = .updatedObjectIDsResultType
batchUpdateRequest.predicate = NSPredicate.init(format: "sector_id == %@", sector.ID)
//Perform the inital update
batchUpdateRequest.propertiesToUpdate = ["sector_id" :sector.ID, "SECTOR_NAME" : sector.SECTOR_NAME]
// Execute the request to do batch update and merge the changes to viewContext, which triggers the UI update
do {
let batchUpdateResult: NSBatchUpdateResult = try BackgroundContext.execute(batchUpdateRequest) as! NSBatchUpdateResult
if let UpdatedObjectIDs = batchUpdateResult.result as? [NSManagedObjectID] {
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: [NSUpdatedObjectsKey: UpdatedObjectIDs],
into: [Context])
}
} catch {
print("Error: \(error)\nCould not batch update existing records.")
return
}
//******************** End of Update Sectors *****************************
}
}
}
}
struct SectorsModel:可映射{
case SECTOR_NAME = "SECTOR_NAME"
case ID = "ID"
case SECTOR = "SECTOR"
}