Skip to content

Instantly share code, notes, and snippets.

@rodydavis
Created January 14, 2018 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodydavis/0100a72eabc8192ee787244816d63977 to your computer and use it in GitHub Desktop.
Save rodydavis/0100a72eabc8192ee787244816d63977 to your computer and use it in GitHub Desktop.
How to save core data order list after rearranging and support for CRUD operations
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
if let appDel = (UIApplication.shared.delegate as? AppDelegate) {
let contxt = appDel.persistentContainer.viewContext
RefreshCoredata()
if fromIndexPath.row > to.row
{
for i in to.row..<fromIndexPath.row
{
songs[i].setValue(i+1, forKey: "orderPosition")
}
songs[fromIndexPath.row].setValue(to.row, forKey: "orderPosition")
}
if fromIndexPath.row < to.row
{
for i in fromIndexPath.row + 1...to.row
{
songs[i].setValue(i-1, forKey: "orderPosition")
}
songs[fromIndexPath.row].setValue(to.row, forKey: "orderPosition")
}
do {
try contxt.save()
} catch {
print("Error Saving New Data Arrangement")
}
RefreshCoredata()
}
}
// Fetch data from data store
func RefreshCoredata() {
let fetchRequest: NSFetchRequest<SongMO> = SongMO.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "orderPosition", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
let context = appDelegate.persistentContainer.viewContext
fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
if let fetchedObjects = fetchResultController.fetchedObjects {
songs = fetchedObjects
}
} catch {
print(error)
}
}
tableView.reloadData()
}
//CRUD Operations
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
if let newIndexPath = newIndexPath {
tableView.insertRows(at: [newIndexPath], with: .fade)
}
case .delete:
if let indX = indexPath {
if let appDel = (UIApplication.shared.delegate as? AppDelegate) {
let contxt = appDel.persistentContainer.viewContext
RefreshCoredata()
contxt.delete(songs[indX.row] as NSManagedObject)
for i in indX.row + 1..<songs.count
{
songs[i].setValue(i-1, forKey: "orderPosition")
}
RefreshCoredata()
tableView.deleteRows(at: [indX], with: .automatic)
do {
try contxt.save()
} catch {
print("Error Saving New Data Arrangement")
}
}
}
case .update:
if let indexPath = indexPath {
tableView.reloadRows(at: [indexPath], with: .fade)
}
default:
tableView.reloadData()
}
if let fetchedObjects = controller.fetchedObjects {
songs = fetchedObjects as! [SongMO]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment