I'm using a SQLite to store data on my application. To view my data I need access the simulator store and execute the querys. The path to my simulators change every time that I install the app on iOS simulator.
for example a path is:
/Users/augustosansoncadini/Library/Developer/CoreSimulator/Devices/56B6ED1A-8B79-4D5C-836D-D813EEA9FE2F/data/Containers/Data/Application/E7A7BCBD-53FE-4C84-8685-7EA1896694FF/Documents
When I create the SQLite context, I put in this path:
static let fileUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("Database.sqlite3")
So, Database.sqlite3 is stored on the documents folder of application folder. But the name of app change when I reinstall app.
Like I need access often this file, I created a Alias to navigate to this folder, but the Alias not works because the code of the app changes, so I wants to create a database file in the Store of iOS simulator but I don't know what path I must put in this line:
static let fileUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("Database.sqlite3")
By "I reinstall app." you mean that you delete from the simulator device and then install it again? or you mean when you re-run the app?
If it's the second one then you will never find your database since you deleted it along with the app.
I think what you really need is a SQLite editor that can handle file path changes automatically, so you needn't have to worry about what the path actually changes to, right?
If the answer is yes, then give SQLiteFlow a try, it's available on App Store, and from it's documentation, it says:
Handle database file name or directory changes. This makes SQLiteFlow can work friendly with your SQLite database in iOS simulator.
Related
I am writing an iOS app and I am trying to use some data/config files. At first I was storing these files in the application bundle and getting the URL path for them like
let path = Bundle.main.url(forResource: "Dasher", withExtension: "json")
This was working fine while running in the simulator but when I would try to run on an actual device I would get file not found errors as it seems like the build was not including the support files.
I then did try to use the documents directory for this
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let fileURL = (dir?.appendingPathComponent(file))!
But it seems every time you build/run you app you get a new application directory so the files I want to read are no longer present.
I feel like I am being obtuse and missing something simple but when I try to look for a solution I just mainly find examples similar to the code above and not how to resolve my use case.
We’re at the tail end of re-writing a legacy Cordova app completely in a new Swift app, the issue we’re facing is when it comes to migration, the Cordova app stores some information on device like refresh tokens and when we force upgrade our users to the new app we want to keep them logged in.
The Cordova app is using a library called LokiJS to do this, and the sql lite db it stores this information in lives inside the /var/mobile/Containers/Data/Application/{UUID}/Library/NoCloud/ directory.
The new Swift app will be using the same bundle identifier so we SHOULD be able to access this directory, although so far I can’t find a way to access this.
There is no directory for /Library/NoCloud within SearchPathDirectory for FileManager, if we use the .libraryDirectory and append "NoCloud" to it we get: "The folder “NoCloud” doesn’t exist." Or even listing the subpathsOfDirectory for .libraryDirectory we get: "The file “Library” couldn’t be opened because there is no such file." The UUID of the app also changes on every install, so no way to know the original full path.
Has anyone had experience doing this before? There must be a way as the Cordova App can read/write to it, but I can’t seem to find any concrete solution and it’s super important that we achieve this, it’s worth noting that our Android app CAN access this Loki db.
SOLVED!
Well after messing about I found I was using the wrong path, I was setting the directory as:
let libraryDirectory = try! FileManager.default.url(for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
and trying to list of contents of that with:
FileManager.default.subpathsOfDirectory(atPath: "\(libraryDirectory.absoluteString)/NoCloud")
When in fact I should have been using,
FileManager.default.subpathsOfDirectory(atPath: "\(libraryDirectory.path)/NoCloud")
which shows me everything :) So to access the contents of the db file I need it is
String(contentsOfFile: "\(libraryDirectory.path)/NoCloud/{db_file_name}", encoding: .utf8)
now, I have DB Data being used on Linux and Window.
I'd like to put it in the iPhone, and use them in my iOS Application.
How to put DB in Sandbox area of my iOS Application ?? And where is the path?
Generally you’d use FileManager method url(for:in:appropriateFor:create:) to build a file URL:
let fileURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("test.sqlite")
So, if you had a blank database in your bundle, you’d copy that initial copy of the database to the above file URL, and then open it using this URL from that point on. Or if you were programmatically creating the database from scratch, you’d again use this resulting file URL.
But don’t save this absolute path anywhere. Always build it programmatically like above.
Note, historically we used to use the .documentsDirectory for things like this (and you’ll see lots of those sorts of answers lingering about on the web). But nowadays we would use the .applicationSupportDirectory. See iOS Standard Directories: Where Files Reside document or the iOS Storage Best Practices video.
I've a simple question to the document directory. I build the folder by the following Swift statement:
open static let DOC_FOLDER : String = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
I store some files and create subfolders there.
And now I'm not sure whether the whole folder structure is deleted automatically if I uninstall the app or does the directory still exists and is only not accessible?
When the user uninstalls the app, the entire sandbox is wiped clean. If the user installs the app again, your app will need to recreate the folder structure again.
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.
=> If you uninstall the app, the folder will be removed
Reference: https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
https://developer.apple.com/library/content/technotes/tn2406/_index.html
I'm working with SQLite.swift.
In the document, the path to the database is:
let path = NSSearchPathForDirectoriesInDomains(
.DocumentDirectory, .UserDomainMask, true
).first!
but i want to import and use an existing database, so i've dragged my existing database to my keyboard extension folder, and create connection to it with path is:
let path = NSBundle.mainBundle().pathForResource("db", ofType:"sqlite3")
So, i've noticed that the first way, the database will be store in /Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Data/PluginKitPlugin/0BC647E4-26F3-4A1F-8271-CC73C96FD197/Documents
and the second way, the database will be store in the app.
/Users/*/Library/Developer/CoreSimulator/Devices/8B1DB861-AA3F-446F-A559-D4727CDB9285/data/Containers/Bundle/Application/E5D9514C-859A-4D4D-A771-A8CE9CDCD3E7/AppName.app/PlugIns/AppNameExt.appex
What's different between these two locations?
The second way might increase the app size because it contains the database?
And if i want to Archive/Submit my App to the AppStore with existing database, is this the only way?
The main difference is that storing the file in the documents folder means you can write (update) it, which is pretty important for a database file. You cannot write to a file in the app bundle.
The usual pattern for using a database in an app is:
Create a pre-seeded database during development and copy it to the app bundle during building.
When running, check if the database file exists and is up-to-date in the documents folder.
If not, copy it from the app bundle.
Open the database in the documents folder and read/write as desired.