Forked from onmyway133/requestForDuplicatedObjects.swift
Created
February 14, 2017 12:08
-
-
Save alemar11/06711bef378f2959a0f5b1f23ddaae25 to your computer and use it in GitHub Desktop.
requestForDuplicatedObjects.swift
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
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