Skip to content

Instantly share code, notes, and snippets.

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 alemar11/06711bef378f2959a0f5b1f23ddaae25 to your computer and use it in GitHub Desktop.
Save alemar11/06711bef378f2959a0f5b1f23ddaae25 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