0

I have a database preloaded in SQLite: "BenedictusCoreData.sqlite".

I add it to the project in XCode.

I modify the BenedictusCoreData.xcdatamodeld to create the entities and attributes with the same name and type as the tables and columns of my sqlite preoloaded.

I run the app in the simulator without writing any additional code. I print the main bundle path and go to the sandbox of the application. And inside /Library/Application Support/ I find the three files: .sqlite .sqlite-shm .sqlite-wal

I open the .sqlite and obviously it is empty, however Xcode has created the tables and the columns with capital letters, adding "Z" to the tablenames and "Z" to the column names, appart from adding new tables "Z_METADATA" "Z_MODELCACHE", "Z_PRIMARYKEY".

At this point, I assume that it is not a good idea just to copy my sqlite from the bundle into the "Application Support" folder and rewrite the file, because the structure is totally different.

So I am lost about how to proceed in order to copy a sqlite preloaded database into CoreData the first time you open the app.

May be you can help me.

jRuMoL
  • 349
  • 1
  • 12

1 Answers1

-1

In the appDelegate you can change the default .xcdatamodel name to the one you added. Then xcode will load your preloaded data .sqlite file instead of the default one. You should search it up there's tutorials on this

Alexander
  • 1,424
  • 18
  • 23
  • May be I didn’t explain myself clearly. The sqlite I have doesn’t have the structure of the sqlite generated by CoreData. If I do what you say, then Xcode will load an sqlite file that has no “Z_METADATA” entity and some other entities and attributes. I don’t think this could work. – jRuMoL Oct 31 '18 at 22:44
  • Xcode will adopt what you input, the entities that you created is limited to the default .xcdatamodel if you point it to your .sqlite file it will adopt yours. Try it, its not complex to point it to the new file. Then you read/write to and from your imported sqllite file. – Alexander Nov 01 '18 at 07:17
  • Excuse me, but I have been searching the web with “Swift change data model” and similar things and I haven’t been able to find what you suggest. – jRuMoL Nov 01 '18 at 07:21
  • Try https://stackoverflow.com/questions/31668137/renaming-coredata-xcdatamodeld-file-and-migration, if I find more I will add to this comment – Alexander Nov 01 '18 at 08:44
  • As far as I have read and search this is to transfer an existing xcdatamodel to a different xcdatamodel, but not to transfer an existing sqlite file into a xcdatamodel. – jRuMoL Nov 01 '18 at 20:06
  • https://stackoverflow.com/questions/16146289/is-it-possilbe-to-have-mutliple-core-data-model-files-to-one-single-xcode-projec this link talks about having multiple .xcdatamodel files, you can read through to see how one would implement a second/ custom model and instead of adding another model, replace the default with yours. – Alexander Nov 02 '18 at 05:28