danutha danutha - 1 year ago 60
iOS Question

How avoid duplication of rows when populating data from core data on a tableView

I'm working on a project that is written in swift 3.0. My requirement is to retrieve and populate the data on a table view which is saved on CoreData that I enter on some text fields, thus once a row is selected I wants to update that record (re-assign values on my text fields and save).
Basically I have an entity named "UserIncome" and it got few attributes. Thus, when I wants to edit the data that I entered, I tap on a row and it'll direct me to the ViewController where I initially entered those data. However when I click the add button and save the data im getting duplicated data and they get populate in my table view. how can I stop this. The code of the class where i save data as follow.

import UIKit
import CoreData

class AddIncomeViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var toDateView: UIView!
@IBOutlet weak var setDateFromView: UIView!
@IBOutlet weak var datePickerTo: UIDatePicker!
@IBOutlet weak var datePickerFrom: UIDatePicker!
@IBOutlet weak var dropDowntableView: UITableView!
@IBOutlet weak var fromTextField: UITextField!
@IBOutlet weak var toTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!

@IBOutlet weak var incomeTypeLabel: UILabel!
@IBOutlet weak var incomeNameTextField: UITextField!

var myArray = ["Recurring Income", "Other Income"]

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var store : UserIncome?
var otherInc : UserIncome?

override func viewDidLoad() {

if let s = store {
incomeNameTextField.text = s.incomeName
amountTextField.text = s.amount
fromTextField.text = s.fromDate
toTextField.text = s.toDate
incomeTypeLabel.text = s.incomeType

if let o = otherInc {
incomeNameTextField.text = o.incomeName
amountTextField.text = o.amount
fromTextField.text = o.fromDate
toTextField.text = o.toDate
incomeTypeLabel.text = o.incomeType



@IBAction func cancelButtonPressed(_ sender: AnyObject) {
navigationController!.popViewController(animated: true)


@IBAction func addButtonPressed(_ sender: AnyObject) {

if store == nil{

let storeDesciption = NSEntityDescription.entity(forEntityName: "UserIncome", in: context)

store = UserIncome(entity:storeDesciption! , insertInto: context)
//otherInc = UserIncome(entity:storeDesciption! , insertInto: context)

if otherInc == nil{
let storeDesciption = NSEntityDescription.entity(forEntityName: "UserIncome", in: context)

otherInc = UserIncome(entity:storeDesciption! , insertInto: context)


store?.incomeName = incomeNameTextField.text
store?.amount = amountTextField.text
store?.fromDate = fromTextField.text
store?.toDate = toTextField.text
store?.incomeType = incomeTypeLabel.text

otherInc?.incomeName = incomeNameTextField.text
otherInc?.amount = amountTextField.text
otherInc?.fromDate = fromTextField.text
otherInc?.toDate = toTextField.text
otherInc?.incomeType = incomeTypeLabel.text

var error : NSError?

(UIApplication.shared.delegate as! AppDelegate).saveContext()
if let err = error{

let a = UIAlertView(title: "Success", message: err.localizedFailureReason, delegate: nil, cancelButtonTitle: "OK")
}else {
let a = UIAlertView(title: "Success", message: "Successfully saved", delegate: nil, cancelButtonTitle: "OK")
// navigationController!.popViewController(animated: true)

Answer Source

You added two different UserIncome objects. You should add one, and share it for your table views.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download