Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created October 30, 2015 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save onmyway133/321606051e26dae214a0 to your computer and use it in GitHub Desktop.
Save onmyway133/321606051e26dae214a0 to your computer and use it in GitHub Desktop.
requestForDuplicatedObjects.swift
func requestForDuplicatedObjects(moc: NSManagedObjectContext) -> NSFetchRequest {
let objectsToKeepRequest = NSFetchRequest(entityName: "Recipe")
// Fetch object IDs representing objects to keep
let ed = NSExpressionDescription()
ed.expression = NSExpression.expressionForEvaludatedObject()
ed.name = "SELF"
ed.expressionResultType = .ObjectIDAttributeType
objectsToKeepRequest.propertiesToFetch = [ed]
// Group by source and exteralID
objectsToKeepRequest.propertiesToGroupBy = ["source", "externalID"]
objectsToKeepRequest.resultType = .DictionaryResultType
let duplicateObjecsRequest = NSFetchRequest(entityName: "Recipe")
// Set up a predicate matching all objects not returned by objectsToKeepRequest
let fre = NSFetchRequestExpression.expressionForFetch(NSExpression(forConstantValue: objectsToKeepRequest), context: NSExpression(forConstantValue: moc), countOnly: false)
duplicateObjectsRequest.predicate = NSPredicate(format: "NOT SELF IN %@", argumentArray: [fre])
return duplicateObjectsRequest
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment