How to use multiple ViewController.swift files with same SQLite database

Hello:


My Xcode project has 29 storyboards and accompanying ViewController.swift files. The first ViewController file contains the code to create a SQLite database with table.


The first storyboard has two data entry fields. All the other 28 have no more than 4 data entry fields. The last will complete adding records for the entire database.


When I save data in the first storyboard I can use a FIND button to check if the data was saved and that is successful. But when I try to save the field data in the subsequent storyboards I get an error --

2019-05-14 14:02:54.326097-0400 No Regrets[6223:1129345] [Common] _BSMachError: port 7703; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"

2019-05-14 14:03:12.007614-0400 No Regrets[6223:1129345] DB Error: 1 "no such table: decisions.db"

2019-05-14 14:03:12.007838-0400 No Regrets[6223:1129345] DB Query: SELECT gain1 FROM decisions.db WHERE id = 1

2019-05-14 14:03:12.008030-0400 No Regrets[6223:1129345] DB Path:

Error: no such table: decisions.db


I have done a lot of research but am unable to find a solution.


Any help will be appreciated.

Replies

decisions.db sounds like a file name, not a table name.


Also, you should probably consider putting data access routines in a shared "model" class instead of a "controller" class.

John,


I tried using the table name "decisions" with same result.


decisions.db sounds like a file name, not a table name.


I really dont know what you mean by putting data access routines in a shared "model" class instead of a "controller" class. I will do some research on that and get back with you. In the meantime, if you have some examples or web resources you can point to I will appreciate it. This is my first Xcode app although I have had many years experience with Oracle.

A basic type of architecture is the MVC (Model-View-Controller). The model has your data. The view is the user interface. The controller is your logic. There are quite a number of variants of this architecture that people have come up with. Apple, as is typically the case, comes up with it own unique approach, the "ViewController". Is it a view or is it a controller?


But the point is to logically separate your app logic in a way that makes sense. Try to make it as un-spaghetti as you can. It sounds like that approach would help.

As already suggested, you’ll need to encapsulate your database code in a helper class which is used by the view controller(s). There is no way any view controller should ever be building SQL queries. That’s a strong code smell. It should definitely be in a model class.


As an aside, why do you have 29 storyboards??? Normally you have one storyboard, which could have 29 view controllers on it. That way you can define segues / relationships among them. That’s kind of the whole point of storyboards.

Thanks Gentlemen:


I don't have a solution worked out as yet but your insights pointed the way to do this.