Posts

Post not yet marked as solved
2 Replies
772 Views
I want to be able to either set a sorting condition for my SQLite Table or sort it every time a new entry is inserted.The SQlite docs say to use query.order(expression)Currently this is what I have:class SQLiteData { var fileURL : URL var tasks : Table var titleDb : Expression var descriptionDb : Expression var dateDb : Expression init() { //... tasks = Table("Tasks") titleDb = Expression("Title") descriptionDb = Expression("Description") dateDb = Expression("Date") tasks.order(dateDb) }but the line tasks.order(dateDb) doesn't seem to have any effect...I also tried it in my uploadData function: func uploadData(_ title: String, _ description: String, _ date: String) { do { let db = try Connection(fileURL.path) try db.run(tasks.create(ifNotExists: true) { t in t.column(titleDb, unique : true) t.column(descriptionDb) t.column(dateDb) }) let insert = tasks.insert(titleDb <- title, descriptionDb <- description, dateDb <- date) _ = tasks.order(dateDb) let rowid = try db.run(insert) } catch { print(error) } }but alas have not had any success doing this either. Any ideas how would I setup the table to always be sorted by dateDb? Thank you.
Posted Last updated
.
Post not yet marked as solved
5 Replies
2.0k Views
Project Information:I have three views: 'Main, AddTask, and TaskTable' and three view controllers: 'MainVC, AddTaskVC, TaskTableVC'. My 'Main' view has an embedded 'UITableView' which is 'TaskTable'. My 'Main' view also has a button that presents a modal view which is 'AddTask'. My 'AddTask' view has a button that dismisses itself. My 'TaskTableVC' has an object named 'taskTable' which is the 'UITableView'Goal:I have been trying to call reloadData on taskTable when the modal view AddTask is dismissed. However, no matter how I tackle this I cannot call reloadData on the taskTable because it always seems to be nil after AddTask appears for the first time.Problem Research:The first thing I did was directly call reloadData() on my taskTable object using completion when the AddTask was dismissed :self.dismiss(animated: true, completion: { TaskTableVC().taskTable.refreshData() })but I quickly learned that I cannot call it directly because the TaskTable view was not loaded and I was creating an instance.Next, I put print statements on my viewDidLoad for my three views (see Project Information). On startup, the first thing that loads is my TaskTable and then my Main. When I click a button on my Main my AddTask loads. So far so good... However, when I dismiss my AddTask, nothing loads. I thought my TaskTable and Main would get loaded but that was not the case. So I tried calling a function refreshData() that was part of MainVC on completion of dismiss in AddTaskVC The function would load the TaskTable view using loadViewIfNeeded:func refreshData() { TaskTableVC().loadViewIfNeeded() }Then in my TaskTableVC under my viewDidLoad I have:override func viewDidLoad() { super.viewDidLoad() print("table view loaded") self.taskTable.reloadData() }Yet the error I continue to get is: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional valueI believe this is because I called TaskTableVC.loadViewIfNeeded() via an instance? Whatever the case may be, I cannot get this to work for the life of me.TL;DR I need to call reloadData on an embedded UITableView when I call dismiss on a modal view.//MainVC: class MainVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. print("main view loaded") } func refreshData() { TaskTableVC().loadViewIfNeeded() } }//AddTaskVC class AddTaskVC: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. print("AddTask view loaded") } //... @IBAction func addTask(_ sender: Any) { //... self.dismiss(animated: true, completion: { MainVC().refreshData() }) } }//TaskTableVC class TaskTableVC : UITableViewController { @IBOutlet var taskTable: UITableView! override func viewDidLoad() { super.viewDidLoad() print("table view loaded") self.taskTable.reloadData() //error here } //... }
Posted Last updated
.