0

I'm having trouble with an answer I received previously on SOF. That is to say I completely understand the concept as it was explained, but I'm having some trouble with the implementation, I seem to constantly lose the functionality for reading/writing values using the format entity.attribute (Created using functions to save duplicating code).

I'm working on a project using CoreData, an entity called 'Studio' and an attribute called 'name' (String). The functionality is simple, the user sets the value of name and it is saved, if the user presses a refresh key a text label is updated with the current value taken from CoreData.

The issue with my code is that I am inadvertantly creating multiple objects and calling them back at random. The solution I was given was to perform a fetch to see if the object exists and create it if it doesn't.

However as I only wish to work with a single instance of any attributes I create and I will have assigned each an initial value I'm wondering if there is a simple implimentation of this concept somebody can recommend?

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.
}


}
2
  • If you have only one entity, Studio, and want only one instance of that, and it has only one attribute (or even a handful), then CoreData is probably overkill - have you considered using NSUserDefaults? It's perfect for such data. Commented Mar 19, 2015 at 8:49
  • I've just started reading up on NSUserDefaults, while I still want to really dive into core Data, it looks like it may be an excellent way for me too good for this app. Thanks, that wasn't even on my radar. Commented Mar 20, 2015 at 3:16

1 Answer 1

1

CoreData objects map to what is stored in the database. In this case, in your getStudioData function, it returns a "live" object. You can read from it, which you do. But you can also update it and write the updates back to CoreData.

You do not do this. Instead, in writeStudioData, you are calling the Studio constructor, with the side effect of adding a new entity into CoreData.

Instead, what you should do is use the instance that you retrieve from getStudioData. If no such entity exists, then you can create a new one. Otherwise, use the existing one. When you want to save your update, you can call managedObjectContext.save() - this will persist your changes to the CoreData store.

Do not create a new one every time you writeStudioData.

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

Comments

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.