I have a question similar to this one but with a little twist.
I am wondering how to return a specific attribute ('weight' in this case) in a fetch request when the core data model is like follows:
Client <-->> Assessment ('Assessment' has a weight attribute)
Here's my Client class:
@objc(Client)
class Client: NSManagedObject {
@NSManaged var name: String
@NSManaged var assessment: NSSet
}
And my Assessment class:
@objc(Assessment)
class Assessment: NSManagedObject {
@NSManaged var weight: Int16
@NSManaged var nsDateOfAssessment: NSDate
@NSManaged var client: Client
}
And here's my fetch request as I have it. I understand that right now my 'weights' array is being filled with managed objects, but what I need is for it to be filled with the ints of 'weight' attribute, and sorted by my date 'nsDateOfAssessment' as described in the comments of the code. I am then in turn using that array to fill a graph.
var client: Client! = nil
var weights = []
func weightFetchRequest() -> NSFetchRequest {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "Assessment")
var error: NSError?
//Something for getting only 'weight' attribute of assessment
//Something for sorting 'weights' array by the date attribute 'nsDateOfAssessment' of 'Assessment'
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
if let results = fetchedResults {
weights = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
println(weights)
return fetchRequest
}
Would greatly appreciate any input or instruction if there is an easier way to do this.