Core Data in Swift: Only Saving Last Object in a for Loop

Swift + Core Data only saving last in loop.

You need to call NSEntityDescription.insertNewObject for each user. You are only calling it once.

Change your code to:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

Alamofire.request(jsonLocation).responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)

for (_, value) in swiftyJsonVar {
let name = value["name"].stringValue
let age = value["age"].stringValue

//print("User: \(name)(\(age)).")

let user = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
user.setValue(name, forKey: "name")
user.setValue(rank, forKey: "age")
}

do {
try context.save()
print("Saved..... ")
} catch {
print("save error")
}
} else {
print("response is nil")
}
}

Swift Core Data only saving relationship to the last in the loop

I don't know if this is the best option for what you have here, but you can add relationships in a nested for loop like below;

//do each day
for i in 0..<30 {

let newChecklist = NSEntityDescription.insertNewObjectForEntityForName(String(Checklist), inManagedObjectContext: moc) as! Checklist
newChecklist.date = NSDate().timeIntervalSince1970
newChecklist.day = Int16(i)

for i in 0..<taskHolder.count {
let items = newChecklist.mutableSetValueForKey("items")
items.addObject(taskHolder[i])
}

do {
try moc.save()
} catch {
print("error")
}
}

Core Data is saving only the last item

You have to create your entity for each value.

NSManagedObjectContext *context = [self managedObjectContext];

for (int i = 0 ; i < 3 ; i++)
{
NSManagedObject *tagsDB = [NSEntityDescription insertNewObjectForEntityForName:@"Tags" inManagedObjectContext:context];
[tagsDB setValue:i forKey:@"ID"];

}

[self saveContext];

Always save the last object

move

var word:WordMO!
word = WordMO(context: getContext())

into your for loop

Swift - How to save entries to core data in a loop

In each pass through your loop, you're throwing away the previous value of seasons and replacing it with a set that contains only one season (the newly created newSeason):

     newSeries.setValue(NSSet(object: newSeason), forKey: "seasons")

Instead of doing that, build up a set of all of the newSeason instances, and then call setValue:forKey: once, with that set.

Your life will be easier, by the way, if you create subclasses of NSManagedObject and get out of the "value for key" business.

One-To-Many relationship core data saves only last object. Others are missing

As of you are setting properties of same object in for loop so that you are getting only last object what you need to do is every time in iteration of for loop you need to create new instance of Favourites then it will store all objects that you have created not the only last one.

for i in 0..<5{
let favourate = Favourites(entity: favourateEentity!, insertInto: managedContext)
favourate.movie = "Some movie" + "\(i)"
favourate.actor = "Some actor" + "\(i)"
favourate.song = "some song" + "\(i)"
favourate.person = person
}


Related Topics



Leave a reply



Submit