i am trying to make a spending control application. However, I cannot pull storages from the dates registered by users on a weekly or monthly basis.Monthly and weekly extraction of user-selected and saved dates to coredata how can I do that ?
if let newSaglik = Int(saglikText.text!){
if let newMarket = Int(marketText.text!){
if let newOther = Int(otherText.text!){
toplam = newSaglik + newMarket + newOther
}
}
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newPendings = NSEntityDescription.insertNewObject(forEntityName: "Pendings", into: context)
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .none
let pickTime = formatter.date(from: dateText.text!)
newPendings.setValue(String(toplam), forKey: "totalPend")
newPendings.setValue(pickTime, forKey: "selectDate")
do {
try context.save()
} catch{
}
}
dateArray.removeAll(keepingCapacity: false)
toplamArray.removeAll(keepingCapacity: false)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Pendings")
fetch.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetch)
if results.count > 0 {
for result in results as! [NSManagedObject]{
if let getPend = result.value(forKey: "totalPend") as? String{
toplamArray.append(getPend)
}
if let getDate = result.value(forKey: "selectDate") as? Date {
dateArray.append(getDate)
}
}
tablevİEW.reloadData()
}
} catch {
}
}```
First of all don't use multiple arrays (like
toplamArrayanddateArray) as data source, use one array ofPendingsinstances.Second of all take advantage of the generic capabilities of
NSManagedObject. The major benefit is to have strong typed property values using dot notation rather than error prone KVC syntaxvalue(forKey:). Also insert a new record withPendings(context: context)rather than with the old non-generic syntax.Assuming the significant part of the
Pendingsclass isadd a function to return a predicate for a given
month,yearandkeyPathThis requires a custom
ErrorenumAs mentioned replace the two arrays representing the data source with one array for example
In the code to fetch the data apply the predicate and assign the result to
filteredPendingsThe predicate for the week depends on what
weeklyactually means. A week number or a start date or something else?