Skip to content

Instantly share code, notes, and snippets.

@adarhef
Created September 7, 2021 12:54
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 adarhef/74117a04915d1a3290535964941f802c to your computer and use it in GitHub Desktop.
Save adarhef/74117a04915d1a3290535964941f802c to your computer and use it in GitHub Desktop.
This gist is some of the code for reference for my video https://www.youtube.com/watch?v=EJdzTJX7AJo
//
// UpdateMergePolicy.swift
// CourseQuery
//
// Created by Adar Hefer on 13/10/2019.
// Copyright © 2019 Adar Hefer. All rights reserved.
//
import CoreData
class UpdateMergePolicy: NSMergePolicy {
override func resolve(constraintConflicts list: [NSConstraintConflict]) throws {
for conflict in list {
guard let databaseObject = conflict.databaseObject,
let conflictingObject = conflict.conflictingObjects.first else {
try super.resolve(constraintConflicts: list)
return
}
switch databaseObject {
case let course as Course:
guard let conflictingCourse = conflictingObject as? Course else {
try super.resolve(constraintConflicts: list)
return
}
conflictingCourse.isAdded = course.isAdded
conflictingCourse.color = course.color
conflictingCourse.justUpdated = true
case let session as CourseSession:
guard let conflictingSession = conflictingObject as? CourseSession else {
try super.resolve(constraintConflicts: list)
return
}
conflictingSession.isAdded = session.isAdded
conflictingSession.isSelected = session.isSelected
conflictingSession.color = session.color
conflictingSession.eventIdentifier = session.eventIdentifier
conflictingSession.justUpdated = true
case let exam as CourseExam:
guard let conflictingExam = conflictingObject as? CourseExam else {
try super.resolve(constraintConflicts: list)
return
}
conflictingExam.isAdded = exam.isAdded
conflictingExam.isSelected = exam.isSelected
conflictingExam.color = exam.color
conflictingExam.eventIdentifier = exam.eventIdentifier
conflictingExam.justUpdated = true
default:
try super.resolve(constraintConflicts: list)
return
}
}
try super.resolve(constraintConflicts: list)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment