Having trouble with core data and having data persist

I’m kind of stuck on what I would assume is a simple solution and I just can’t figure out what to do.

I’m basically creating an expenses application for iOS and I’ve gotten to the point where I need to add up all the total expenses from my UITableView list.

 private var items:[Shoe] = []
 private var appDelegate = UIApplication.shared.delegate as! AppDelegate
 private var managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

 // Start of calculating the gain or loss for the individual item
        let buyDouble: Double? = Double(buyToSave)
        let sellDouble: Double? = Double(sellToSave)
        let feeDouble: Double? = Double(feeToSave)
        let feeInt = 1-(feeDouble!/100)
        let profit = (feeInt*sellDouble!) - buyDouble!
        let profitNSNumb = profit as NSNumber
        let profitString = currencyFormatter.string(from: profitNSNumb)
        
        let shoe = Shoe(entity: Shoe.entity(), insertInto: self.managedContext)
        shoe.name = nameToSave
        shoe.buyPrice = buyToSave
        shoe.sellPrice = sellToSave
        shoe.size = sizeToSave
        shoe.fee = feeToSave
        shoe.profitLoss = profitString
        shoe.quantity = Int16(shoeQuantity)
        shoe.sum = shoe.sum + profit

 // Save the data
        self.appDelegate.saveContext()
        
        // Reloads the UITableView
        self.shoeList.reloadData()
        self.viewDidLoad()

I believe my logic is correct right? Im not sure why I keep start at $0 every time I quit my application and open it back up again?

ex:

ITEM #…EXPENSE

Item 1…$100

Item 2…$200

Item 3…$50

Total Expense: $350

Then when I close the app and start it up again:

ITEM #…EXPENSE

Item 1…$100

Item 2…$200

Item 3…$50

Total Expense: $350 <---- I want it to start at $350 and not $0

@johnbelarmino1 Thanks very much for your question!

I see the following issues:

  1. Not sure why you’re creating an instance of the appDelegate. Your Managed Object Context (MOC) should be passed over to your other ViewControllers via Dependency Injection. In other words, you should be passing your MOC over from one VC to the other. Don’t use the AppDelegate to obtain a reference to it.

  2. Your line where you call self.appDelegate.saveContext() does absolutely nothing. It has no reference to the MOC, so I believe you’re trying to call saveContext() on the MOC and not the AppDelegate. I think this may be a typo, but still.

  3. You should not be calling self.viewDidLoad(). You just don’t do that. viewDidLoad() is a method in the VC that is called only once and that is when the VC appears for the first time. You should in my opinion perhaps consider calling ViewWillAppear/ViewDidAppear which are called whenever the screen is presented to the user. On a side note, you should try to understand ViewControllers, and their lifecycle a bit better. You need to rethink what it is you’re trying to do, and plan out your app a little bit.

The above are some fundamental issues that you should resolve first.

I hope this helps!

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.