How to get Core data sqlite file location - swift

I am trying to find the sqlite file location created by core data. I am using the below code:
print("Documents Directory: ", FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last ?? "Not Found!")
And it's showing me the result: ile:///var/mobile/Containers/Data/Application/148690BC-21F4-4239-99B2-5F947023B414/Library/Application Support
But I can't find any sqlite file in the location.
Can any one please help me to solve the issue. I am using Swift 4 and testing my code in iPhone 6sPlus

Unless your app is document-based, the default location for your app's sqlite database is its Application Support directory, not Documents. Change .documentDirectory to .applicationSupportDirectory.

Related

Cloud Kit - Synching image

I'm currently sharing data between devices using core data and cloud kit.
Currently I have a "Picture" entity which stores the filename, the date it was added and a relation to another entity.
The details in core data successfully syncs, how would I go about syncing the image file.
The file is saved in the documents folder and I can get its file path using
let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName)
Research seems to show I need to use CKAsset to upload/download the file but how do I go about doing this?
Resolved the issue by saving the image as data into core data, can then convert it back to an image when displaying

read/write database path using sqlite.swift

I'm just starting out trying sqlite.swift and databases with swift. I have prepared a database with tables and preloaded with data. I wish to select data and insert data from within the app.
The problem is that I don't understand where (in my project) to put my database.db file in order for the app to find it.
my connection code:
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
db = try Connection("\(path)/database.db")
} catch {
db = nil
print ("Unable to open database")
}
In terms of where this file should go, I would suggest the “application support directory”. See File System Programming Guide: Where You Should Put Your App’s Files, which says:
Put user data in Documents/. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later.
Put app-created support files in the Library/Application support/ directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.
Also see the iOS Storage Best Practices video.
The above is for iOS. If inquiring about macOS, see File System Programming Guide: The Library Directory Stores App-Specific Files.
But regardless of the OS, the technique for referencing the application support directory is largely the same:
do {
let fileURL = try FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("database.db")
db = try Connection(fileURL.path)
} catch {
db = nil
print("Unable to open database: \(error)")
}

Date still in Core Data app after I delete the .sqlite files

I have (finally) managed to get prototype app using Core Data to persist to an sqlite file. Quitting and restarting I see the same data. So all seems well.
I want to now start from scratch again so I went to delete the sqlite persisted data. I set up it's location as :
open lazy var applicationDocumentsDirectory: NSURL = {
// The directory the application uses to store the Core Data store file.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1] as NSURL
}()
let url = self.applicationDocumentsDirectory.appendingPathComponent("TrainingDiary.sqlite")
which places the .sqlite files in ~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Documents
There are four files in there: iChats, TrainingDiary.sqlite, TrainingDiary.sqlite-shm and TrainingDiary.sqlite-wal
I close my app. Delete all there files. When I reopen my app the full Core Data model is still there and when I look back in this directory the files are back.
What am I missing ?
EDIT:
I've figured out whats happening though I still don't quite understand why this is how it works. The above code specifies where the Core Data should be saved:
which places the .sqlite files in
~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Documents/TrainingDiary.sqlite
This it does however I've now found that the app also saves the following
~/Library/Containers/uk.me.stevenlord.Core-Data-Binding/Data/Library/Application Support/Core Data Binding/Core_Data_Binding.sqlite
My XCode9 project is called "Core Data Binding". If I delete this file and start up my app I get an app with no data as expected.
So the persistent store is not going to where I specified. So still confused.
I'm missing something still. But something different

How to find the path of sqlite.sql file and print it?

I know this question already answered before but when I tried to print the path for the sqlite file of my application as below I got the path but could not go to it in finder.
I used below code to get the path
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String
print(paths)
The path I got is as below:
/var/mobile/Containers/Data/Application/5412CDDE-8D40-46B2-A61D-65065796F7CE/Documents
when I try to copy and paste it in GOTO it can not find it
please advise me
In case you are using NSPersistantContainer, you get the URL(s):
persistentContainer.persistentStoreCoordinator.persistentStores.first!.url!
when I try to copy and paste it in GOTO it can not find it
that path is on the Simulator and not on the local Finder.
Duplicate of: Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone
or
Duplicate of: How to view the data in sqlite file running in iphone application?
How to find the path of sqlite.sql file and print it?
accessing your sqlite database is described here:
Copy Sqlite DataBase in Swift does not work properly
Use this in your swift class to get the location of sqlite database for your application running in iOS simulator.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print(urls[urls.count-1] as URL)
You will need SQLite database browser to look after the database. We can only locate sqlite database file only for application running in simulator but not in the iPhone directly.

How to hide SQLite db files so they are not seen in iTunes file sharing

I have file sharing enabled on my app and so, when you view the documents folder in iTunes, there are the Core Data sqlite files siting there just waiting to be fiddled with by the user.
I have found a few discussions on this but, surprisingly, no one seems to address the concerns I have.
Some say to 'move' them to the library folder - in a custom sub-directory, and another says just rename the files prefixing each with a period.
Both options sound lovely, but these are essential OS files!
So my first question is, if you do either of these things (with NSFileManager.defaultManager() - I presume), will the app just automatically find them afterwards? ...or is there a specific 'way' in which you do (either of) them ...so that the app finds them afterward?
Any responses, if you could demonstrate using Swift rather than Objective C, that would be appreciated! Thanks, :)
Figured it out:
To anyone wondering the same thing (which I have seen many doing),
In my app delegate > in the 'persistentStoreCoordinator' lazy variable,
I changed the following line of code:
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyAppName.sqlite")
to the following two lines of code:
let library = NSFileManager.defaultManager().URLsForDirectory(.LibraryDirectory, inDomains: .UserDomainMask)[0] as NSURL
let url = libary.URLByAppendingPathComponent("MyAppName.sqlite")
This would be the syntax for Swift 3 / 4:
let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0] as NSURL
let url = library.appendingPathComponent("MyAppName.sqlite")