将重新排序的核心数据对象保存回iOS iOS swift 3

时间:2017-05-16 23:30:55

标签: ios swift core-data

我已经实现了包含7条记录的核心数据的数据库。我已经实现了集合视图以3X3格式显示数据。我还实现了收集单元的重新排列,在此期间我将更新NSManagedObject。我想将重新排列的对象保存回我的数据库,以便用户重新排列的内容对设备保持持久性。请找到我的以下代码。

override func viewDidLoad() {
    super.viewDidLoad()

    let managedContext = CoreDataManager.sharedManager.persistentContainer.viewContext

    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Sample")
    do {

        people = try managedContext.fetch(fetchRequest)

    } catch let error as NSError {

        print("Could not fetch. \(error), \(error.userInfo)")
    }

 func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let managedContext = CoreDataManager.sharedManager.persistentContainer.viewContext

    let temp = people.remove(at: sourceIndexPath.item)
    people.insert(temp, at: destinationIndexPath.item)

    do {

        try managedContext.save()

    } catch let error as NSError {

        print("Could not save. \(error), \(error.userInfo)")
    }


}

如果我遗漏任何事情,请告诉我

0 个答案:

没有答案