Update All Value in One Attribute Core Data

Update all value in one attribute Core Data

Try following example it might be helpful.Replace you entity name.

  var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)

var context: NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Params")
var params = NSEntityDescription.insertNewObjectForEntityForName("Params", inManagedObjectContext: context) as! NSManagedObject

if let fetchResults = appDel.managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [NSManagedObject] {

if fetchResults.count != 0{
for (var v = 0 ; v < fetchResults.count ; v++) {
var managedObject = fetchResults[v]

println(fetchResults)
managedObject.setValue("-1", forKey: "value")
context.save(nil)

}
}
}

Programmatically Update an attribute in Core Data

The Apple documentation on using managed objects in Core Data likely has your answer. In short, though, you should be able to do something like this:

NSError *saveError;
[bookTwo setTitle:@"BarBar"];
if (![managedObjectContext save:&saveError]) {
NSLog(@"Saving changes to book book two failed: %@", saveError);
} else {
// The changes to bookTwo have been persisted.
}

(Note: bookTwo must be a managed object that is associated with managedObjectContext for this example to work.)

What's the syntax for updating existing attributes in core data compared to saving a new object in core data?

  • The first syntax inserts a new record – you have to save the context afterwards.

  • The second syntax fetches existing data and updates a record.

However to update a specific record you have to add a predicate and most likely you don't want to update the name and the gender attributes

let name = "John Doe"
let fetchRequest : NSFetchRequest<Person> = Person.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "name == %@", name)
let results = try context.fetch(fetchRequest)
if let container = results.first {
// container.name = some_string
// container.gender = some_string
container.last_occupation = custom_object
try context.save()
context.refresh(transformableContainer, mergeChanges: false)
}

How to update values of core data objects with same attributes but different values?

You need to fetch that value that you wan to update then change its value and commit instead here you are inserting the new record that is not correct.
See the following stack question:

How to update existing object in Core Data?

What is the most efficient way to update the value of all or some managed objects in Core Data in Objective-C

Depending on what you want to update the values to, you could fetch an array of objects and call setValue:forKey: on the array, which would set the value for every object in the array. e.g.:

//fetch managed objects
NSArray *objects = [context executeFetchRequest:allRequest error:&error];
[objects setValue:newValue forKey:@"entityProperty"];
//now save your changes back.
[context save:&error];

Setting up the fetch request should just require the entity you want, leave the predicate nil so that you get all instances back.

Change Value of Attribute Through Core Data Relationship

The reason you are getting this error is because you are trying to force unwrap a nil value. You should instead use an if let statement:

if let date dateObject.valueForKey("theDate") as? NSDate {
if date == newDate {
...
}
}

How to update attribute with core data

I've got something working right now, but I'm not sure it's the most efficient way:

I've created a class called Stories:

import UIKit
import CoreData

@objc(Stories)
class Stories: NSManagedObject {
@NSManaged var pagina:String
@NSManaged var plaats:String
@NSManaged var naam:String
}

Now my btnSave action is this:

@IBAction func brnSave(sender: AnyObject) { 
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Stories")
var results:NSArray = context.executeFetchRequest(request, error: nil) as [Stories]
if (results.count > 0) {
println(results.count)
if (results.count >= txtPagina.text.toInt()) {
for res in results{
var data = res as Stories
println(data.plaats)
if (data.pagina == txtPagina.text) {
data.plaats = txtPlaats.text
data.naam = txtNaam.text
context.save(nil)
println("Page \(data.pagina) updated")
}
}
} else {
// Save new Story
var ent = NSEntityDescription.entityForName("Stories", inManagedObjectContext: context)
var newStory = Stories(entity: ent!, insertIntoManagedObjectContext: context)
newStory.pagina = txtPagina.text
newStory.plaats = txtPlaats.text
newStory.naam = txtNaam.text
context.save(nil)
println("New story saved")
// Save new Story end

}

} else {

// Save new Story
var ent = NSEntityDescription.entityForName("Stories", inManagedObjectContext: context)
var newStory = Stories(entity: ent!, insertIntoManagedObjectContext: context)
newStory.pagina = txtPagina.text
newStory.plaats = txtPlaats.text
newStory.naam = txtNaam.text
context.save(nil)

println(newStory)
println("New story saved")
// Save new Story end

}
}

The code works, but doesn't seem perfect.
Is there a way to make this more efficient? When updating the array I'm basically rewriting the whole coredata table instead of just updating the changed data. I'm can only imagine what this does performance wise when there's a lot more data!

How to change value of an attribute of all entities in Core Data at once?

Agrees with coverback...lets say you want to fetch all the objects from the entity named "Test":

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test"
inManagedObjectContext:context];
NSError *error;
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

fetchObjects array contains all the objects in "Test" entity

Update an object on core data swift 4

Please read the error message. It's very clear. In setValue you are passing a String (via String Interpolation) rather than expected Int or NSNumber

object.setValue(jobId, forKey: "jobId")

or

object.setValue(NSNumber(value: jobId), forKey: "jobId")

But the best and recommended way is dot notation

object.jobId = jobId


Related Topics



Leave a reply



Submit