Created
January 12, 2018 07:29
-
-
Save Lancewer/d0855ee3c3efe2b06e662463764f1371 to your computer and use it in GitHub Desktop.
[FetchPropertiesOnDemand] Fetch part of properties in an managed object #CoreData #Fetch
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
When you don't want to get all propeties form a managed object, then use this sample code to get part of properties you want. |
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
let briefInfoFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ClothItem") | |
briefInfoFetchRequest.resultType = .dictionaryResultType | |
briefInfoFetchRequest.returnsObjectsAsFaults = false | |
let desc = NSExpressionDescription() | |
desc.name = "mainPhoto" | |
desc.expression = NSExpression(forKeyPath: "mainPhotoURLString") | |
desc.isStoredInExternalRecord = true | |
desc.expressionResultType = .stringAttributeType | |
briefInfoFetchRequest.propertiesToFetch = ["name", desc] | |
do { | |
itemBriefInfoArray = try managedContext!.fetch(briefInfoFetchRequest) | |
print(itemBriefInfoArray) | |
}catch let error as NSError { | |
print("Fetch item failed:\(error), \(error.userInfo)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment