user4184036
user4184036

Reputation:

How do I check if core data class is nil. in SWIFT

I have created an Entity called Status with an attribute called statusUpdate. I want the user to be able to create a status for the first time which would then be saved in core data, then when the status is next updated to save over the old status in core data. Here is what i've done:

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Status", inManagedObjectContext: context) as NSManagedObject

var request = NSFetchRequest(entityName: "Status")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: &self.error)

if results?.count > 0 {
    for result: AnyObject in results! {
        if var oldStatus = result.valueForKey("statusUpdate") as? String {
            println("Current User fetched status from Core Data")
            var newStatus = self.updateStatusText.text
            result.setValue(newStatus, forKey: "statusUpdate")
        } else if result.valueForKey("statusUpdate") == nil {
            var newStatus = self.updateStatusText.text
            newUser.setValue(newStatus, forKey: "statusUpdate")
            println("Status uploaded for the first time")
        }
    }
}

context.save(&self.error)

The problem is the else if seems to always run. I want it to basically only run of the Status entity is nil (so it will only run for the first time somebody updates a status) and then when the status is updated after that it will run just the regular if statement.

I'm not sure what i'm doing wrong???

Thanks in advance.

Upvotes: 1

Views: 2458

Answers (2)

pbasdf
pbasdf

Reputation: 21536

Line 3 of your code inserts a new Status object, so each time this code is executed, you are adding an extra object. The newly inserted object will have a statusUpdate of nil (most likely).

When you execute the fetch, in line 6, this new Status object will be included in the results, together with any objects you have saved previously. So when you iterate through all the results, there will always be one for which statusUpdate is nil.

I would restructure your code to do the fetch first, establish whether there are any results. If so, update the statusUpdate; if not, create the new object and set the statusUpdate field. Something like this:

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext!

var request = NSFetchRequest(entityName: "Status")
request.returnsObjectsAsFaults = false
var results = context.executeFetchRequest(request, error: &self.error)

if let resultsArray = results { // explicitly test for nil results
    if resultsArray.count > 0 {
        // count should be at most one, but just in case we will iterate through:
        for result: AnyObject in resultsArray {
            println("Current User fetched status from Core Data")
            var newStatus = self.updateStatusText.text
            result.setValue(newStatus, forKey: "statusUpdate")
        }
    else { // resultsArray is empty, so insert a new object
        var newStatus = self.updateStatusText.text
        let newUser = NSEntityDescription.insertNewObjectForEntityForName("Status", inManagedObjectContext: context) as NSManagedObject
        newUser.setValue(newStatus, forKey: "statusUpdate")
        println("Status uploaded for the first time")
    }
} else { // fetch returned nil, so log an error:
    println("Nil returned by fetch")
}    
context.save(&self.error)

Upvotes: 1

user3435374
user3435374

Reputation: 1296

   if (var oldStatus = result.valueForKey("statusUpdate") as? String) != nil{

        println("Current User fetched status from Core Data")
        var newStatus = self.updateStatusText.text
        result.setValue(newStatus, forKey: "statusUpdate")


    }else {

        var newStatus = self.updateStatusText.text
        newUser.setValue(newStatus, forKey: "statusUpdate")
        println("Status uploaded for the first time")
    }

Upvotes: 0

Related Questions