import Foundation
class Persistence
{
let partiesKey = "parties"
let namesKey = "names"
func saveParty(party: Party)
{
let userDefaults = UserDefaults.standard
var parties = fetchParties()
parties.append(party)
let data = NSKeyedArchiver.archivedData(withRootObject: parties)
userDefaults.set(data, forKey: partiesKey)
userDefaults.synchronize()
}
func fetchParties() -> [Party]
{
let userDefaults = UserDefaults.standard
let parties = userDefaults.object(forKey: partiesKey) as? Data
if let parties = parties
{
return NSKeyedUnarchiver.unarchiveObject(with: parties) as! [Party]
}
else
{
return [Party]()
}
}
func deleteParty(party: Party)
{
let userDefaults = UserDefaults.standard
var parties = fetchParties()
}
func saveName(partyName: String)
{
var name = fetchName()
name.append(partyName)
let userDefaults = UserDefaults.standard
userDefaults.set(name, forKey: namesKey)
userDefaults.synchronize()
}
func fetchName() -> [String]
{
let userDefaults = UserDefaults.standard
let name = (userDefaults.value(forKey: namesKey) as? [String])
return name ?? [String]()
}
}
Hi, I am very new to iOS programming. I am working on todo list for my first iOS app. I am trying to delete table view cell that is added by fetching and saving. I was able to save data and show name in the tableview cell. How do we delete table view cell in UserDefault? func deleteParty() method will be called where the tableView cell is like this:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if(editingStyle == .delete)
{
persistence.deleteParty(party: (parties?[indexPath.row])!)
//tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
}
Related
I am trying to show an information which is in Core Data, on UITableViewCell.
I could get the information, but the information wasn't shown on UITableViewCell.
When I set the information on CoreData, I use Modal View then.
I tried to use UITableView.reload() but I couldn't show the information on UITableViewCell.
Please let me know how to show the information when I back from modal view.
This class is about showing the information on UItableView.
import UIKit
import CoreData
protocol FriendListTableViewDelegate {
func reloadTable()
}
class FriendListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FriendListTableViewDelegate{
#IBOutlet weak var friendListTableView: UITableView!
var friends:[FriendBasicInfo] = []
override func viewDidLoad() {
super.viewDidLoad()
getData()
}
// Disable to effect the reload
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadTable()
}
// fetch the information from CoreData
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
friends = try context.fetch(FriendBasicInfo.fetchRequest())
} catch {
print("error")
}
}
func reloadTable() {
friendListTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let friendCell = tableView.dequeueReusableCell(withIdentifier: "FriendListCell") as! FriendListTableViewCell
let friendName = friendCell.viewWithTag(1) as? UILabel
let friendImage = friendCell.viewWithTag(2) as? UIImageView
friendName?.text = friends[indexPath.row].name
friendImage?.image = friends[indexPath.row].photo?.toImage()
return friendCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
}
This class is to get the information from CoreData
import UIKit
import XLPagerTabStrip
import Eureka
import CoreData
import ImageRow
class InputFriendInforViewController: FormViewController, IndicatorInfoProvider {
var itemInfo: IndicatorInfo = "Info"
var friendPhoto: UIImage?
var friendName: String = ""
var friendBirthday: Date?
var friendGender: String = ""
var friendListTableViewDelegate: FriendListTableViewDelegate!
override func viewDidLoad() {
super.viewDidLoad()
form +++
Section("Friend Information")
<<< ImageRow(){
$0.title = "Image"
$0.sourceTypes = [.PhotoLibrary, .SavedPhotosAlbum, .Camera]
$0.value = UIImage(named: "noImage")
$0.onChange { [unowned self] row in
self.friendPhoto = row.value!
}
}
<<< TextRow(){ row in
row.title = "Name"
row.placeholder = "Enter Name here"
}.onChange { name in
self.friendName = name.value!
}
<<< DateRow(){ row in
row.title = "Birthday"
row.value = Date(timeIntervalSinceReferenceDate: 0)
}.onChange {date in
self.friendBirthday = date.value!
}
<<< PushRow<String>(){row in
row.title = "Gender"
row.options = ["Male","Female","Other"]
}.onChange {gender in
self.friendGender = gender.value!
}
+++ Section()
<<< ButtonRow() {
$0.title = "SAVE"
}.onCellSelection {_, _ in
self.saveInfo()
}
}
// MARK: - IndicatorInfoProvider
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
return itemInfo
}
// save friend Info for Core Data
func saveInfo (){
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let friendEntity = NSEntityDescription.entity(forEntityName: "FriendBasicInfo", in: managedContext)!
let friendInfo = NSManagedObject(entity: friendEntity, insertInto: managedContext)
// make unique user ID
let friendUid = NSUUID().uuidString
// Image Data UIImage to png Data
let pngImage = self.friendPhoto?.toPNGData()
friendInfo.setValue(friendUid, forKey: "userID")
friendInfo.setValue(pngImage, forKey: "photo")
friendInfo.setValue(self.friendName, forKey: "name")
friendInfo.setValue(self.friendBirthday, forKey: "birthday")
friendInfo.setValue(self.friendGender, forKey: "gender")
do {
try managedContext.save()
self.dismiss(animated: true, completion:nil)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
}
This class is about UITableViewCell
import UIKit
class FriendListTableViewCell: UITableViewCell {
#IBOutlet weak var sampleImageView: UIImageView!
#IBOutlet weak var sampleLabel:UILabel!
}
V/r,
As you are using an extra data source array just reloading the table view doesn't consider the new inserted item.
There are a few options
Use NSFetchedResultsController. It updates the UI automatically when the context was saved.
On dismiss insert the new item into the data source array and a new row into the table view.
Observe NSManagedObjectContextDidSaveNotification and insert the item as described in 2.
Refetch the entire data and reload the table view.
The options are in order of efficiency. Version 1 is the most efficient one.
Side note:
viewWithTag is horribly old-fashioned. You got outlets, use them for example
cell.sampleLabel!.text = friends[indexPath.row].name
Your FriendListViewController TableView will reflect any updates to any FriendBasicInfo Entity which was fetched within getData() method. To present a new inserted FriendBasicInfo Entities to the database you have to execute a new fetch with getData() method.
Solution:
func reloadTable() {
getData()
friendListTableView.reloadData()
}
Alternative solution
Advanced monitoring of a fetched entities can be done with NSFetchedResultsController Delegate, this controller will automatically update the FriendListViewController tableview for any updated, inserted or deleted entities.
I'm trying to create a function to check the Connectivity when the App loads. If a internet connection is detected, the app should download the JSON data and save the array in UserDefaults, then proceed to the UITableView methods. However, if the internet connection is not found, the app should recover the array on USerDefault to populate another array, then proceed to UItableView methods.
The problem I'm facing, is that when the compiler goes trough UserDefault line to be able to save the array, the app crashes immediately. What I'm doing wrong?
Compiler Error:
) for key backupSaved'
*** First throw call stack: (0x18257ad8c 0x1817345ec 0x18257ac6c 0x1825b1d08 0x1824e730c 0x1824e5a60 0x1825b2080 0x182515cec
0x1825b2080 0x1825b2304 0x182518d6c 0x182518588 0x182518c54
0x1825bc218 0x1825bf8a0 0x182edaaf4 0x102a794c0 0x102a7452c
0x102e8d314 0x102e45b7c 0x103da11dc 0x103da119c 0x103da5d2c
0x182523070 0x182520bc8 0x182440da8 0x184425020 0x18c45d758
0x102a756b0 0x181ed1fc0) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
[FIXED]ViewController:
import UIKit
import Kingfisher
import Alamofire
var arrCerveja = [Cerveja]()
var arrBackup = [Cerveja]()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
//TableView DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if Connectivity.isConnectedToInternet {
return arrCerveja.count
} else {
return arrBackup.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! TableViewCell
if Connectivity.isConnectedToInternet {
let model = arrCerveja[indexPath.row]
cell.labelName.text = model.name
cell.labelDetail.text = "Teor alcoólico: \(model.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(model.image_url)")!, cacheKey: model.image_url)
cell.imageViewCell.kf.setImage(with: resource, placeholder: UIImage(named: "icons8-hourglass-48"), options: nil, progressBlock: nil, completionHandler: nil)
return cell
} else {
let model = arrBackup[indexPath.row]
cell.labelName.text = model.name
cell.labelDetail.text = "Teor alcoólico: \(model.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(model.image_url)")!, cacheKey: model.image_url)
cell.imageViewCell.kf.setImage(with: resource, placeholder: UIImage(named: "icons8-hourglass-48"), options: nil, progressBlock: nil, completionHandler: nil)
return cell
}
}
//TableView Delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if Connectivity.isConnectedToInternet {
performSegue(withIdentifier: "segueId", sender:arrCerveja[indexPath.row])
} else {
performSegue(withIdentifier: "segueId", sender:arrBackup[indexPath.row])
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueId" {
let des = segue.destination as? TableViewDetalhes
//.item possui uma propriedade instanciada na TelaDetalheProdutos
des?.item = (sender as? Cerveja)
//Segue para CollectionView Categorias
}
}
struct Connectivity {
static let sharedInstance = NetworkReachabilityManager()!
static var isConnectedToInternet:Bool {
return self.sharedInstance.isReachable
}
}
override func viewDidAppear(_ animated: Bool) {
if Connectivity.isConnectedToInternet {
print("Connected")
getApiData { (cerveja) in
arrCerveja = cerveja
//Backup
do{
let data = try JSONEncoder().encode(arrCerveja)
UserDefaults.standard.set(data, forKey: "backupSaved")
//
self.tableView.reloadData()
}catch{print(error)
}
}
} else {
print("No Internet")
do{
if let savedData = UserDefaults.standard.value(forKey: "backupSaved") as? Data {
arrBackup = try JSONDecoder().decode([Cerveja].self, from: savedData)
self.tableView.reloadData()
}
}catch{
print(error)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//SetupNavBarCustom
navigationController?.navigationBar.setupNavigationBar()
}
}
Model:
struct Cerveja:Decodable{
let name:String
let image_url:String
let description:String
let tagline:String
let abv:Double
let ibu:Double?
}
The array should be endcoded to Data before saving , as it's array of custom objects
do {
....... write
let data = try JSONEncoder().encode(arr)
UserDefaults.standard.set(data, forKey: "backupSaved")
// save as data
....... read
if let savedData = UserDefaults.standard.value(forKey: "backupSaved") as? Data {
let savedArr = try JSONDecoder().decode([Cerveja].self, from: savedData)
// use the array here
}
}
catch {
print(error)
}
//
Since now you encode & decode
struct Cerveja:Codable {--}
//
Also I don't vote for saving in userDefaults consider CoreData
I am try to save and retrieve notes data with custom object called Sheet.
But I am having crashes when it runs. Is this the correct way to do it or is there any other ways to solve this?
The Sheet Class
class Sheet {
var title = ""
var content = ""
}
Here is the class for UITableViewController
class NotesListTableVC: UITableViewController {
var notes = [Sheet]()
override func viewDidLoad() {
super.viewDidLoad()
if let newNotes = UserDefaults.standard.object(forKey: "notes") as? [Sheet] {
//set the instance variable to the newNotes variable
notes = newNotes
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return notes.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "notesCELL", for: indexPath)
cell.textLabel!.text = notes[indexPath.row].title
return cell
}
// Add new note or opening existing note
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editNote" {
var noteContentVC = segue.destination as! NoteContentVC
var selectedIndexPath = tableView.indexPathForSelectedRow
noteContentVC.note = notes[selectedIndexPath!.row]
}
else if segue.identifier == "newNote" {
var newEntry = Sheet()
notes.append(newEntry)
var noteContentVC = segue.destination as! NoteContentVC
noteContentVC.note = newEntry
}
saveNotesArray()
}
// Reload the table view
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
// Deleting notes
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
notes.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}
// Save the notes
func saveNotesArray() {
// Save the newly updated array
UserDefaults.standard.set(notes, forKey: "notes")
UserDefaults.standard.synchronize()
}
}
And where should I call the saveNotesArray function?
You are trying to save an array of custom objects to UserDefaults. Your custom object isn't a property list object You should use Codable to save non-property list object in UserDefaults like this.
Swift 4
Custom Class
class Sheet: Codable {
var title = ""
var content = ""
}
ViewController.swift
class ViewController: UIViewController {
var notes = [Sheet]()
override func viewDidLoad() {
super.viewDidLoad()
getSheets()
addSheets()
getSheets()
}
func getSheets()
{
if let storedObject: Data = UserDefaults.standard.data(forKey: "notes")
{
do
{
notes = try PropertyListDecoder().decode([Sheet].self, from: storedObject)
for note in notes
{
print(note.title)
print(note.content)
}
}
catch
{
print(error.localizedDescription)
}
}
}
func addSheets()
{
let sheet1 = Sheet()
sheet1.title = "title1"
sheet1.content = "content1"
let sheet2 = Sheet()
sheet2.title = "title1"
sheet2.content = "content1"
notes = [sheet1,sheet2]
do
{
UserDefaults.standard.set(try PropertyListEncoder().encode(notes), forKey: "notes")
UserDefaults.standard.synchronize()
}
catch
{
print(error.localizedDescription)
}
}
}
You give answer to the question that you ask.
App crash log.
[User Defaults] Attempt to set a non-property-list object ( "Sheet.Sheet" )
Official Apple info.
A default object must be a property list—that is, an instance of (or
for collections, a combination of instances of): NSData, NSString,
NSNumber, NSDate, NSArray, or NSDictionary.
If you want to store any other type of object, you should typically
archive it to create an instance of NSData. For more details, see
Preferences and Settings Programming Guide.
One of the possible solution:
class Sheet : NSObject, NSCoding{
var title:String?
var content:String?
func encode(with aCoder: NSCoder) {
aCoder.encodeObject(self.title, forKey: "title")
aCoder.encodeObject(self.content, forKey: "content")
}
required init?(coder aDecoder: NSCoder) {
self.title = aDecoder.decodeObject(forKey: "title") as? String
self.content = aDecoder.decodeObject(forKey: "content") as? String
}
}
Save
userDefaults.setValue(NSKeyedArchiver.archivedDataWithRootObject(sheets), forKey: "sheets")
Load
sheets = NSKeyedUnarchiver.unarchiveObjectWithData(userDefaults.objectForKey("sheets") as! NSData) as! [Sheet]
The code you posted tries to save an array of custom objects to NSUserDefaults. You can't do that. Implementing the NSCoding methods doesn't help. You can only store things like Array, Dictionary, String, Data, Number, and Date in UserDefaults.
You need to convert the object to Data (like you have in some of the code) and store that Data in UserDefaults. You can even store an Array of Data if you need to.
When you read back the array you need to unarchive the Data to get back your Sheet objects.
Change your Sheet object to :
class Sheet: NSObject, NSCoding {
var title: String
var content: String
init(title: String, content: String) {
self.title = title
self.content = content
}
required convenience init(coder aDecoder: NSCoder) {
let title = aDecoder.decodeObject(forKey: "title") as! String
let content = aDecoder.decodeObject(forKey: "content") as! String
self.init(title: title, content: content)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(title, forKey: "title")
aCoder.encode(content, forKey: "content")
}
}
into a function like :
func loadData() {
if let decoded = userDefaults.object(forKey: "notes") as? Data, let notes = NSKeyedUnarchiver.unarchiveObject(with: decoded) as? [Sheet] {
self.notes = notes
self.tableView.reloadData()
}
}
and then call :
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.loadData()
}
saveNotesArray can be called after new Notes added with :
func saveNotesArray() {
// Save the newly updated array
var userDefaults = UserDefaults.standard
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: notes)
userDefaults.set(encodedData, forKey: "notes")
userDefaults.synchronize()
}
Im working on a project in swift 3.0 and I have two UITableViews where I set data fetched from a core-data module entity called UserIncome. As these data will be populated in two UItableViews in a single UIViewController (filtering based on a String value in the ViewWillAppear delegate method),once a row is been deleted in one UITableView, its array automatically gets updated by the other tableView's objects too. But once I click the back button and come back to the same UIViewController all seems fine. My requirement is to update the UItableView once a row is been deleted so as the core data module. The code as bellow. What am I missing here?
import UIKit
import CoreData
class MyIncomesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var recurringIncomeTableView: UITableView!
#IBOutlet weak var otherIncomeTableView: UITableView!
//var myIncomeType : String?
var stores = [UserIncome] ()
var other = [UserIncome] ()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var rowTbl : Int!
var rowTbl2 : Int!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
stores.removeAll()
other.removeAll()
let request = NSFetchRequest <NSFetchRequestResult> (entityName: "UserIncome")
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request) as! [UserIncome]
print("Results from the fetch request are : ", request)
// check data existance
if results.count>0 {
print("results are :", results.count)
for resultGot in results {
//lets check if the data is available and whether the loop is working by printing out the "name"
if let incName = resultGot.incomeName {
print("expence name is :", incName)
//set the value to the global variable as to filter the arrays
let myIncomeType = resultGot.incomeType
if myIncomeType == "Recurring Income"{
stores += [resultGot]
print("my recurring income array is : \(stores)")
}else if myIncomeType == "Other Income"{
other += [resultGot]
print("my other income array is : \(other)")
}
}
}
self.recurringIncomeTableView.reloadData()
self.otherIncomeTableView.reloadData()
}
}catch{
print("No Data to load")
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView.tag == 1 {
let cell: RecuringIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "recurringIncomeCell") as! RecuringIncomeTableViewCell
let store = stores [indexPath.row]
cell.incomeNameLabel.text = store.incomeName
cell.amountLabel.text = store.amount
return cell
}
else {
let cell: OtherIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "otherIncomeCell") as! OtherIncomeTableViewCell
let otherIncomes = other [indexPath.row]
cell.incomeNameLabel.text = otherIncomes.incomeName
cell.amountLabel.text = otherIncomes.amount
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//performSegue(withIdentifier: "editStore", sender: nil)
if tableView.tag == 1 {
rowTbl = tableView.indexPathForSelectedRow?.row
print("current row in tbl 1 is : ",rowTbl)
}else {
rowTbl2 = tableView.indexPathForSelectedRow?.row
print("current row in tbl 2 is : ",rowTbl2)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editRecurringIncome"{
let v = segue.destination as! AddIncomeViewController
let indexPath = self.recurringIncomeTableView.indexPathForSelectedRow
let row = indexPath?.row
v.store = stores[row!]
}else if segue.identifier == "editOtherIncome" {
let t = segue.destination as! AddIncomeViewController
let indexPath = self.otherIncomeTableView.indexPathForSelectedRow
let row = indexPath?.row
t.store = other [row!]
}
}
//
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
print("delete delegate being activated")
return true
}
//For remove row from tableview & object from array.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if tableView.tag == 1 {
if editingStyle == .delete {
let task = stores [indexPath.row]
context.delete(task)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
stores = try context.fetch(UserIncome.fetchRequest())
print("Stores deleted from indexPath",stores)
}catch{
print("fail")
}
recurringIncomeTableView.reloadData()
}
self.recurringIncomeTableView.reloadData()
} else if tableView.tag == 2 {
if editingStyle == .delete {
let task = other[indexPath.row]
print("task on otherTblView is : ",task)
context.delete(task)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
otherIncomeTableView.reloadData()
do {
other = try context.fetch(UserIncome.fetchRequest())
print("Stores deleted from indexPath",other)
}catch{
print("fail")
}
}
self.otherIncomeTableView.reloadData()
}
tableView.reloadData()
}
}
you need to delete task like this way
let task = stores [indexPath.row]
context.delete(task)
stores.removeAtIndex(indexPath.row) // i think you forget this line
(UIApplication.shared.delegate as! AppDelegate).saveContext()
try this,hope it will help you
A core data object doesn't really contain any information. It has a pointer to a context and an ID, so when you ask it for information it goes to the store to ask. If the object is deleted from the context then the manage object that you have stored in your array will no longer work and will crash. This is why you should never retain NSManagedObjects. Either
a) Copy the values from core data into an a different object. When you want to delete an object you have to delete it from both the store and the copy that you are retaining. If new objects are inserted, or they are deleted from some other source outside of you viewController it will not update (but also no crash).
b) Use a NSFetchedResultsController and update the results when the values change. This will give a delegate to tell you when changes happen. So all you have to do it delete the object from the store and then the fetchedResultsController will tell you when to remove it.
Im working on a project in swift 3.0 and Im fetching data from core data on to two tableViews namely;'recurringIncomeTableView', and 'otherIncomeTableView'. However when 'commit editingStyle' function is activated (once I slide the row), I can deleted the particular row in 'recurringIncomeTableView'. But when i slide a row in 'otherIncomeTableView' and pressed delete, in the line 'let task = stores [indexPath.row]' causing the problem and the app is crashing. The code as bellow.
import UIKit
import CoreData
class MyIncomesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var recurringIncomeTableView: UITableView!
#IBOutlet weak var otherIncomeTableView: UITableView!
//var myIncomeType : String?
var stores = [UserIncome] ()
var other = [UserIncome] ()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
self.recurringIncomeTableView.reloadData()
self.otherIncomeTableView.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
stores.removeAll()
other.removeAll()
let request = NSFetchRequest <NSFetchRequestResult> (entityName: "UserIncome")
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request) as! [UserIncome]
print("Results from the fetch request are : ", request)
// check data existance
if results.count>0 {
print("results are :", results.count)
for resultGot in results {
//lets check if the data is available and whether the loop is working by printing out the "name"
if let incName = resultGot.incomeName {
print("expence name is :", incName)
//set the value to the global variable as to filter the arrays
let myIncomeType = resultGot.incomeType
if myIncomeType == "Recurring Income"{
stores += [resultGot]
print("my recurring income array is : \(stores)")
}else if myIncomeType == "Other Income"{
other += [resultGot]
print("my other income array is : \(other)")
}
}
}
self.recurringIncomeTableView.reloadData()
self.otherIncomeTableView.reloadData()
}
}catch{
print("No Data to load")
}
}
#IBAction func addIncome(sender: UIButton) {
print("Add Income Button Clicked")
performSegue(withIdentifier: "ShowAddIncomeVC", sender: nil)
// Do whatever you need when the button is pressed
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.recurringIncomeTableView {
print("recurringIncomeTableView count is ", stores.count)
return stores.count
}else {
print("otherIncomeTableView count is ", other.count)
return other.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.recurringIncomeTableView {
let cell: RecuringIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "recurringIncomeCell") as! RecuringIncomeTableViewCell
let store = stores [indexPath.row]
cell.incomeNameLabel.text = store.incomeName
cell.amountLabel.text = store.amount
//cell.textLabel?.text = myExpensesArray[indexPath.row]
return cell
}else {
let cell: OtherIncomeTableViewCell = tableView.dequeueReusableCell(withIdentifier: "otherIncomeCell") as! OtherIncomeTableViewCell
let otherIncomes = other [indexPath.row]
cell.incomeNameLabel.text = otherIncomes.incomeName
cell.amountLabel.text = otherIncomes.amount
//cell.textLabel?.text = myExpensesArray[indexPath.row]
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//performSegue(withIdentifier: "editStore", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "editRecurringIncome"{
let v = segue.destination as! AddIncomeViewController
let indexPath = self.recurringIncomeTableView.indexPathForSelectedRow
let row = indexPath?.row
v.store = stores[row!]
}else if segue.identifier == "editOtherIncome" {
let t = segue.destination as! AddIncomeViewController
let indexPath = self.otherIncomeTableView.indexPathForSelectedRow
let row = indexPath?.row
t.store = other [row!]
}
}
//
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
//For remove row from tableview & object from array.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete {
**let task = stores [indexPath.row]**
context.delete(task)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
stores = try context.fetch(UserIncome.fetchRequest())
}catch{
print("fail")
}
}
tableView.reloadData()
}
}
As per your Core data fetch request code.
You have to store core data object in your store array than & than you can delete that object directly form store array.
You need to fetch object like this :
// Initialize Fetch Request
let fetchRequest = NSFetchRequest()
// Create Entity Description
let entityDescription = NSEntityDescription.entityForName("UserIncome", inManagedObjectContext: self.managedObjectContext)
// Configure Fetch Request
fetchRequest.entity = entityDescription
store = try self.managedObjectContext.executeFetchRequest(fetchRequest)
After getting all data you have to filter your array with your requirement and display it in tableview I have just give example how to show that data in tableview.
Show your data in cell like this :
var data: NSManagedObject = store[indexPath.row] as NSManagedObject
Cell.textLabel?.text = data.valueForKeyPath("Name") as? String
Delete your data as per your code :
let task = stores [indexPath.row]
context.delete(task)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
it will help you to understand flow of core data with tableview.