CoreData. Some data have a 'nil' and zero value

670 views Asked by At

I don't know why, but some of my data has nil and zero value. My entity consists of 1 NSDate and 5 Float Attributes. I have four sliders to set values for four of them one is computed (arithmetic mean for that other 4) and the NSDate stores just the current date and time.

var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var newHowDoYouFeel = NSEntityDescription.insertNewObjectForEntityForName("HowDoYouFeel", inManagedObjectContext: context) as! NSManagedObject

   // var howDoYouFeels  = [HowDoYouFeel]()

    var totalAfter = (slider1!.value + slider2!.value + slider3!.value + slider4!.value)/4

    newHowDoYouFeel.setValue(totalBefore, forKey: "totalBefore")
    newHowDoYouFeel.setValue(focusBefore, forKey: "focusBefore")
    newHowDoYouFeel.setValue(moodBefore, forKey: "moodBefore")
    newHowDoYouFeel.setValue(relaxBefore, forKey: "relaxBefore")
    newHowDoYouFeel.setValue(energyBefore, forKey: "energyBefore")
    newHowDoYouFeel.setValue(NSDate(), forKey: "date")

    newHowDoYouFeel.setValue(totalAfter, forKey: "totalAfter")
    newHowDoYouFeel.setValue(slider1!.value, forKey: "focusAfter")
    newHowDoYouFeel.setValue(slider2!.value, forKey: "moodAfter")
    newHowDoYouFeel.setValue(slider3!.value, forKey: "relaxAfter")
    newHowDoYouFeel.setValue(slider4!.value, forKey: "energyAfter")
    newHowDoYouFeel.setValue(NSDate(), forKey: "date")

    context.save(nil)

Here is the code fetching the data:

 var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        var context:NSManagedObjectContext = appDel.managedObjectContext!
        var newHowDoYouFeel = NSEntityDescription.insertNewObjectForEntityForName("HowDoYouFeel", inManagedObjectContext: context) as! NSManagedObject
        var howDoYouFeels  = [HowDoYouFeel]()
        var fetchRequest = NSFetchRequest(entityName: "HowDoYouFeel")
        fetchRequest.returnsObjectsAsFaults = false
        var error : NSError?



        if let howDoYouFeels = context.executeFetchRequest(fetchRequest, error: &error) as? [HowDoYouFeel]{

            println("count is: \(howDoYouFeels.count)")



               if howDoYouFeels.count > 0 {
                for howDoYouFeel in howDoYouFeels {
                 println(howDoYouFeel)
                }
               }
}
}
        else {
            println("empty")

        }
    }

    else {
        println("Fetch failed: \(error)")

    }

And the NSManaged class looks like this:

import Foundation
import CoreData

class HowDoYouFeel: NSManagedObject {

@NSManaged var totalBefore: Float
@NSManaged var totalAfter: Float
@NSManaged var focusBefore: Float
@NSManaged var focusAfter: Float
@NSManaged var moodBefore: Float
@NSManaged var moodAfter: Float
@NSManaged var relaxBefore: Float
@NSManaged var relaxAfter: Float
@NSManaged var energyBefore: Float
@NSManaged var energyAfter: Float
@NSManaged var date: NSDate

}

But then when I try to print it some of the data looks like this:

<Scroll_tab.HowDoYouFeel: 0x7fe4624380c0> (entity: HowDoYouFeel; id: 0x7fe462428430 <x-coredata:///HowDoYouFeel/t289EB596-9019-441F-86F0-7225F85A46B23> ; data: {
    date = nil;
    energyAfter = 0;
    energyBefore = 0;
    focusAfter = 0;
    focusBefore = 0;
    moodAfter = 0;
    moodBefore = 0;
    relaxAfter = 0;
    relaxBefore = 0;
    totalAfter = 0;
    totalBefore = 0;

And some data is ok have all the correct values.

What am I doing wrong?

1

There are 1 answers

2
Anthony On

Well, all of your primitives are 0 because that is their default value before they are set. Your date is an object, not a primitive, which is why it is nil and not 0 (Nil indicates the absence of a value for object references).

I have no idea what's going on with newHowDoYouFeel, but for the howDoYouFeels array, it looks like one or more of the howDoYouFeel objects returned by

context.executeFetchRequest(fetchRequest, error: &error)

hasn't had any of its members set. What you see in that last code excerpt is a howDoYouFeel that was created but left otherwise untouched until the print statement.

Or at least that's my novice opinion. I'd check to see if your fetch request (either the code for it or your implementation of it) could be causing the problem.