String Returning Multiple values in Swift

BlueRad

I have a CoreData entity named 'Studio' with an attribute named 'name' with an NSManagedObject subclass created.

My app designed for a simple process, enter a name into a text box, and press 'save' and the name is saved into Studio.name - Press 'Update' and a text label is refreshed to show the value of Studio.name

However, it is not functioning as expected, if I, for example, enter the name 'Stack' and save the update I see 'Stack' in the text label, if i then enter 'Overflow' save/update the label reads 'Overflow', If i update it a third time to 'Swift' save/update the label again reads 'Stack'.

From there updates will give one of the three values seemingly at random. Force quitting the app and relaunching it shows that the data is being saved to Core Data as pressing the update button will return a random previous value.

My question is, how does this happen with a string? (Shouldn't it only hold one value at a time?) How can I correct this so it will only hold a single value and any subsequent values simply overwrite the previous value?

My code follows.

import UIKit
import CoreData

class ViewController: UIViewController {

@IBOutlet weak var studioBox: UITextField!

@IBOutlet weak var nameLabel: UILabel!

@IBAction func saveData(sender: AnyObject) {
    var studio = writeStudioData()
    studio.name = studioBox.text
}
@IBAction func Update(sender: AnyObject) {
    var studio = getStudioData()
    nameLabel.text = studio.name
}


func getStudioData() -> Studio {
    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let managedContext = appDelegate.managedObjectContext!
    let request = NSFetchRequest(entityName: "Studio")
    request.returnsObjectsAsFaults = false

    let result = managedContext.executeFetchRequest(request, error: nil) as [Studio]
    return result[0]
}

func writeStudioData () -> Studio {
    let appDelegate =
    UIApplication.sharedApplication().delegate as AppDelegate
    let managedContext = appDelegate.managedObjectContext!
    let entityDescription = NSEntityDescription.entityForName("Studio", inManagedObjectContext: managedContext)
    let result = Studio(entity: entityDescription!, insertIntoManagedObjectContext: managedContext)
    return result
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}
Kevin

You don't have one object, you are creating a new object every time. As you aren't including a sort descriptor with your fetch request, the order you get them, and thus the corresponding name, is unspecified, meaning it could be any of them.

You could either perform a fetch first in writeStudioData to see if there's already an object, only creating one if there isn't, or you could create one object and keep it around in a property.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related