如何将重新排序的Tableview单元格保存到核心数据中

问题描述 投票:0回答:1

所以我有我的tableview,当我对行重新排序时,它会更新tableview和所有内容,但不会保存到core data

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {


    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let movedCampSites = itemName[sourceIndexPath.item]
    itemName.remove(at: sourceIndexPath.item)
    itemName.insert(movedCampSites, at: destinationIndexPath.item)
    context.delete(itemName[sourceIndexPath.row])
    context.insert(itemName[destinationIndexPath.row])

    do
    {
        try context.save()
    }
    catch
    {
        print("Could not move rows")
    }



}

例如,如果我注释掉并运行它,一切都会进行到context.insert(itemName[destinationIndexPath.row]),它将移动行并删除该行并保存到core data,但是context.insert不会保存新的行位置。由于某种原因,它正在运行我的catch块,因为我收到错误无法移动行。

这里是控制台中的某些错误。

2018-07-22 09:27:01.884925-0700搜索栏核心数据[67957:5383630] [错误]错误:(1555)唯一约束失败:ZTITLE.Z_PKCoreData:错误:(1555)唯一约束失败:ZTITLE.Z_PK无法移动行

谢谢。

swift core-data tableview reorderlist
1个回答
0
投票

这是我对这个问题的看法:

就像“ vadian”说的那样,当您创建一个对象时,您会为附加到核心数据的数组添加一个属性,像这样,

var itemsArray: [NSManagedObject] = []

save(sortOrder: itemsArray.count, item: itemTextField.text!) //Calling the save method wherever you need to, catching for example a UITextField user input

private func save(sortOrder: Int, item: String) {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    guard let entity = NSEntityDescription.entity(forEntityName: "CoreDataBaseName", in: context) else { return }

    let itemManagedObject = NSManagedObject(entity: entity, insertInto: context)

    itemManagedObject.setValue(sortOrder, forKeyPath: "sortOrder")
    itemManagedObject.setValue(item, forKeyPath: "item")

    do {
        try context.save()
        itemsArray.append(itemManagedObject)

    } catch let error as NSError {
        print("Could not save item to Core Data: \(error)")
    }
}

然后我们进入tableView的moveRowAt方法,

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    let movedItem = itemsArray[sourceIndexPath.row]

    itemsArray.remove(at: sourceIndexPath.row)
    itemsArray.insert(movedItem, at: destinationIndexPath.row)

    //This 'for-in loop' is at the heart of the solution
    for (i, item) in itemsArray.enumerated() {
        item.setValue(i, forKey: "sortOrder")
    }

    do {
        try context.save()
    } catch let error as NSError {
        print("Could not save/persist Core Data items in moveRowAt: \(error)")
    }
}

...在所有内容的结尾处,我们使用重新获取顺序,

private func fetch() {

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let context = appDelegate.persistentContainer.viewContext
    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "CoreDataBaseName")

    let sortDescriptor = NSSortDescriptor(key: "sortOrder", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    do {
        itemsArray = try context.fetch(fetchRequest)
    } catch let error as NSError {
        print("Could not fetch Core Data entities: \(error)")
    }
}

您可以通过一百万种不同的方式来改善这个想法(这总是很受欢迎的,但这是解决方案的教学示例。

我希望它可以帮助某个人!

© www.soinside.com 2019 - 2024. All rights reserved.