UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Loading .sqlite file into Core Data

Forums > Swift

Hey,

I am currently working on an app that store recipes using core data. I also want the users to be able to share the recipes they have saved, with other people using airdrop, messages, etc. I am not sure if this is the correct way to do it but I have managed to export an entry from core data as a .sqlite file. And I am currently stuck on how to import the .sqlite back into the app (on another device), such that that entry is added to the existing entries in database. I have done some research and I found that you can use the addPersistenceStore() function for such things. Is this the correct approach or is there another way of doing this? I would appreiciate any help. And if someone has some example code that would be perfect.

I, currently have this code for importing the sqlite file into core data.

persistenceController.viewContext.persistentStoreCoordinator?.addPersistentStore(ofType: ".sqlite", configurationName: nil, at: path, options: [:])

Thanks

2      

Exporting to and importing from .sqlite is a bit heavy going. Yes, it can be done, but I would consider a different route first.

If it is sharing between my own devices then I would use CloudKit. To access that in during development you would have to pay for the Apple's Developer account. Putting the persistent store in the CloudKit would simplify the sharing between devices.

If your not willing or wanting to pay for the Developer's account, and this is the way I would share recipes with others, you can output the Core Data information to a JSON file, and keep / share / send the JSON file. There are number of examples on the internet of how to manage saving and loading data between CoreData and JSON.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.