3

I have preloaded data from a .csv file into coredata. I am fetching the data in the following way

var places:[Places] = []

in viewDidLoad :

if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
            let fetchRequest = NSFetchRequest(entityName: "Places")
            do{
                places = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Places]
            }

            catch let error as NSError{
                print("Failed to retrieve record: \(error.localizedDescription)")
            }
        }

In the data there is an attribute isFavorite of type String whose initial value is false. I am changing the value of isFavorite on button click. I want to save the changes made by the user. How can i make this change persistent ?

Here is my button action

@IBAction func addToFavourites(sender: AnyObject) {

        cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell
        if cell.isFavouriteLabel.text! == "false" {
            cell.isFavouriteLabel.text = "true"

        }else if cell.isFavouriteLabel.text == "true"{
            cell.isFavouriteLabel.text = "false"

        }
}

Basically i want to set the value of places.isFavourite = cell.isFavoriteLabel.text and save to core data

EDIT : if i try this inside my button action method

if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

            let place : Places = Places()
            place.isFavourite = cell.isFavouriteLabel.text
             do{
                try managedObjectContext.save()
            } catch let error as NSError{
                print(error)

            }
          }

i am getting an error : Failed to call designated initializer on NSManagedObject class

if i simply add this code in the button action method

places.isFavourite = cell.isFavouriteLabel.text

i get this error : [Places] does not have a member named isFavourite

3 Answers 3

3

Your current code is:

if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {

            let place : Places = Places()
            place.isFavourite = cell.isFavouriteLabel.text
             do{
                try managedObjectContext.save()
            } catch let error as NSError{
                print(error)

            }
          }

That would create a new place (if it worked), but you need to update an existing one.

You have the places returned from managedObjectContext.executeFetchRequest.

So you need to get something like places[index_of_the_cell_in_question].isFavourite = cell.isFavouriteLabel.text

and then managedObjectContext.save().

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

1 Comment

thanx places[sender.tag].isFavourite = cell.isFavouriteLabel.text did the trick
0

Use the save function of the NSManagedObjectContext:

places.isFavourite = cell.isFavoriteLabel.text

var error: NSError?
if managedObjectContext.save(&error) != true {
    // Error
}

3 Comments

i can't seem to access isFavourite using places.isFavourite in my button action method
Dependent on your implementation, you should be able to access the corresponding Places object after clicking the button. Your button action method has a sender parameter, maybe you can use that.
i can access isFavorite if places is declared like this var places : Places!
0

This is simple as this:

  • Find the entry you want to modify in places then save the core data context.

    func saveContext () {
        if let moc = self.managedObjectContext {
            var error: NSError? = nil
            if moc.hasChanges && !moc.save(&error) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                println("Unresolved error \(error), \(error!.userInfo)")
                abort()
            }
        }
    }
    

I suggest you used a manager to insert, fetch and delete entry in your core data.

import Foundation
import CoreData

class CoreDataHelper: NSObject {

    class var shareInstance:CoreDataHelper {
        struct Static {
            static let instance:CoreDataHelper = CoreDataHelper()
        }
        return Static.instance
    }

    //MARK: - Insert -
    func insertEntityForName(entityName:String) -> AnyObject {
        return NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: self.managedObjectContext!)
    }

    //MARK: - Fetch -
    func fetchEntitiesForName(entityName:String) -> NSArray {
        ...
    }

    //MARK: - Delete -
    func deleteObject(object:NSManagedObject) {
        self.managedObjectContext!.deleteObject(object)
    }

    // MARK: - Core Data Saving support -
    func saveContext () {
        if let moc = self.managedObjectContext {
            var error: NSError? = nil
            if moc.hasChanges && !moc.save(&error) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                println("Unresolved error \(error), \(error!.userInfo)")
                abort()
            }
        }
    }

Hop this can help you

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.