CoreData Add Object

I made an application that uses basic data. I made a function that saves 1 or 2 values ​​/ writes data to the main data. This is the following method:

    func saveName(name: String) {

    let myDate:NSDate = NSDate()
    let context = self.fetchedResultsController.managedObjectContext
    let entity = self.fetchedResultsController.fetchRequest.entity!
    let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as NSManagedObject

    if markCell == true {
        newManagedObject.setValue(name, forKey: "markedCell")
        markCell = false
    }

    else {
        newManagedObject.setValue(name, forKey: "name")
        newManagedObject.setValue(myDate, forKey: "datum")
    }

    // Save the context.
    var error: NSError? = nil
    if !context.save(&error) {
        abort()
    }
}

There is a failure in the function cellForRowAtIndexPathif markCell == true. If markCell == false(step in else), everything works fine.

If I run this function:

    func saveName(name: String) {

    let myDate:NSDate = NSDate()
    let context = self.fetchedResultsController.managedObjectContext
    let entity = self.fetchedResultsController.fetchRequest.entity!
    let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entity.name!, inManagedObjectContext: context) as NSManagedObject

        newManagedObject.setValue(name, forKey: "markedCell")
        markCell = false
        newManagedObject.setValue(name, forKey: "name")
        newManagedObject.setValue(myDate, forKey: "datum")

    // Save the context.
    var error: NSError? = nil
    if !context.save(&error) {
        abort()
    }
}

not crashing, but I also added value markedCell. I want to add a value to markedCellif bool is set to true (the user clicked the button β†’ bool will be set to trueand func saveNamewill be called).

Load data from master data (create UITableViewCell):

//Get task
    let context = self.fetchedResultsController.managedObjectContext
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as  NSManagedObject
    var taskString:NSString
    taskString = object.valueForKey("name") as String
    cell.textLabel!.text = object.valueForKey("name") as? String
    //Set accessory type
    var request:NSFetchRequest = NSFetchRequest(entityName: "Person")
    request.predicate = NSPredicate(format:"markedCell = %@", taskString)
    var results : [NSManagedObject] = context.executeFetchRequest(request, error: nil) as [NSManagedObject]
    if (results.count > 0) {
        //Element exists
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        println("Cell is marked")
    }
    else {
        //Doesn't exist
        cell.accessoryType = UITableViewCellAccessoryType.None
        println("Cell isn't marked")
    }
+4
source share
1

, , markedCell Core Data, name / datum .

, else, :

markedCell == nil //this is allowed in your Core Data model
name != nil
datum != nil

, else, :

markedCell != nil
name == nil
datum == nil

. , , .

0

All Articles