Swift编程新手,并且在使用Core Data时出现问题。 我试图删除通过Int64号码链接到主实体的2个其他实体中的条目。
GameInfo - 主实体。有比赛号码,球队名称,得分,日期属性等。 HomeSubstitution& AwaySubstitution-两队换人。每场比赛可以超过一场。将游戏编号作为每个记录集中的属性。
我与实体建立了关系并制作了删除规则= Cascade。
图形数据模型: core data model
我可以删除删除GameInfo记录集但它已经消失,但HomeSubstitution和AwaySubstitution记录仍然存在。当您开始新游戏时,旧数据现在链接到新游戏。 即,继续查看游戏,并使用新游戏显示已删除的GameInfo中的替换。
这是我的代码。
@objc lazy var fetchedResultsController: NSFetchedResultsController<GameInfo> = {
let fetchRequest: NSFetchRequest<GameInfo> = GameInfo.fetchRequestGame()
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "gameNumber", ascending: true)]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Fetch game
let gameRow = fetchedResultsController.object(at: indexPath)
let gameNumberDelete = gameRow.gameNumber
print("game row to delete: \(gameRow)")
print("game number to delete: \(String(describing: gameNumberDelete))") //This works and I get the game number
// Delete game
gameRow.managedObjectContext?.delete(gameRow)
}
}
我尝试使用类似的代码来执行fetchedHomeController和fetchedAwayController,这些代码引用了引用不同实体的fetchedResultsController和NSPredicate(在其他类中用于查看游戏),但是我得到了错误。
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Fetch game
let gameRow = fetchedResultsController.object(at: indexPath)
let gameNumberDelete = gameRow.gameNumber
print("game row to delete: \(gameRow)")
print("game number to delete: \(String(describing: gameNumberDelete))")
// Delete game
gameRow.managedObjectContext?.delete(gameRow)
//Delete home substitutions
let fetchRequestHome = NSFetchRequest<NSFetchRequestResult>(entityName: "HomeSubstitution")
fetchRequestHome.predicate = NSPredicate(format: "homeGameLink == %i", gameNumberDelete)
fetchRequestHome.returnsObjectsAsFaults = false
print("fetchRequestHome: \(fetchRequestHome)")
do {
try self.fetchedHomeController.performFetch()
let homeGameDelete = fetchedHomeController.object(at: indexPath)
print("homeGameDelete: \(homeGameDelete)")
homeGameDelete.managedObjectContext?.delete(homeGameDelete) //Error here to do with NSFetchedResultsController.
} catch {
let fetchError = error as NSError
print("Unable to Perform Fetch Request")
print("\(fetchError), \(fetchError.localizedDescription)")
}
}
}
我怀疑删除次数超过我选择的行数且NSFetchedResultsController不允许这样做。
编辑: 弄清楚了。使用Raj Joshi在Delete Object from Core Data中给出的例子。
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Fetch game
let mocGame = getContext()
let gameRow = fetchedResultsController.object(at: indexPath)
let gameNumberDelete = gameRow.gameNumber
print("game row to delete: \(gameRow)")
print("game number to delete: \(String(describing: gameNumberDelete))")
// Delete game
gameRow.managedObjectContext?.delete(gameRow)
do {
try mocGame.save()
print("saved Game Deletion!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
//Delete substitutions based on gameNumber
deleteHomeGameData(link: gameNumberDelete)
deleteAwayGameData(link: gameNumberDelete)
}
}
func deleteHomeGameData(link: Int64) -> Void{
let mocHome = getContext()
let fetchRequestHome = NSFetchRequest<NSFetchRequestResult>(entityName: "HomeSubstitution")
fetchRequestHome.predicate = NSPredicate(format: "homeGameLink == %i", link)
do {
let result = try? mocHome.fetch(fetchRequestHome)
let resultData = result as! [HomeSubstitution]
//Loop through substitutions and delete.
for object in resultData {
mocHome.delete(object)
}
do {
try mocHome.save()
print("saved Home substitution deletion!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
}
func deleteAwayGameData(link: Int64) -> Void{
let mocAway = getContext()
let fetchRequestAway = NSFetchRequest<NSFetchRequestResult>(entityName: "AwaySubstitution")
fetchRequestAway.predicate = NSPredicate(format: "awayGameLink == %i", link)
do {
let result = try? mocAway.fetch(fetchRequestAway)
let resultData = result as! [AwaySubstitution]
//Loop through substitutions and delete.
for object in resultData {
mocAway.delete(object)
}
do {
try mocAway.save()
print("saved Away substitution deletion!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
}
func getContext () -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
return appDelegate.persistentContainer.viewContext
}