Created
September 7, 2021 12:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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