0

I am trying to fetch data from CoreData and return it as an array for charting in a popular charting library. The code below runs fine and I can print the data to the console, however I want to fill self.hkdataBase with the entries within CoreData and Return it. Any idea what I am doing wrong ?

class ViewController: UIViewController {
var hkdataBase = [HKdataBase]()

....

func fetchCoreData () -> NSArray {

//Setting up Core Data
    var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
    var request = NSFetchRequest(entityName: "HKdataBase")
    request.returnsObjectsAsFaults = false

//Fetching Data
    self.hkdataBase = context.executeFetchRequest(request, error: nil)! as [HKdataBase]
    if hkdataBase.count > 0 {
        for i in hkdataBase {

        **I WANT TO FILL self.hkdataBase WITH i ELEMENTS HERE **  ->  println(i.hr_data) WORKS FINE


        }
    } else {
        println("No results")
    }
}
**return self.hkdataBase with elements**
}

UPDATE 1: I want to Return an Array from the fetched data in CoreData. The hr_data are Heart Rate measures and hr_date are the associated NSDate. The HKdataBase looks like this:

import Foundation import CoreData

class HKdataBase: NSManagedObject {

@NSManaged var hr_date: NSDate
@NSManaged var hr_data: NSNumber

}

5
  • hr_data in your model is a number, not an array. Why would you expect an array? Please explain what you're trying to accomplish with your data model. I would guess what you actually want is a second entity that has a to-many relationship with the main entity. The entities in a core data database are like tables / objects. e.g. You might have an entity called Person that has a name, age, and height. Each person might be associated with 0 or more other objects, e.g. a Book Commented Dec 10, 2014 at 19:08
  • Yes, that is my problem, how do I make it an array? Ok so my CoreData model is wrong? I am trying to return an array so that I can use the array to graph the numbers Commented Dec 10, 2014 at 19:13
  • Can't help you if you don't explain what you're trying to accomplish. (Add this detail by editing your question, not by commenting) Commented Dec 10, 2014 at 19:16
  • 1
    The result of context.executeFetchRequest(...)! as [HKdataBase] is already an array. What else do you need? Commented Dec 10, 2014 at 19:51
  • if I return self.hkdataBase I just get an empty array [, , , , , , , , , , , , , , , ]. How do I fill it? Commented Dec 10, 2014 at 20:01

1 Answer 1

1

After the fetch, you already have your managed objects in self.hkdataBase. Managed objects are KVC compliant, so to get just one kind of value out if it is quite simple.

var hrData = (self.hkdataBase as NSArray).valueForKey("hr_data") as NSArray

You now have an array containing your hr_data, presumably an array of NSNumber objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so, so much Mundi ! I have been trying to get this to work for 3 whole days now and left work (startup) today quite demoralized(live in Europe) . You brought me one step closer to our goal - THANK YOU !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.