How to initialize a Realm DB - ios

I want my app to ship out with a pre-initialized DB.
In SQL, I would create a DB, do the necessary inserts, pg_dump to a file, and load from that file. I'm sure Realm has an equivalent method, but I'm not sure what it is and I couldn't find it in the documentation.

(Disclaimer: I work for Realm)
There's no pg_dump-like functionality in Realm, but you can distribute a pre-built Realm file along with your app and that'll work just fine. :)
At the moment, the best way to create a pre-made Realm file is to simply make a small sample app to generate and then populate the file, but we're working on adding that functionality to Realm Browser as well.

Related

How to correctly configure path for the Realm DB-file (<db_name>.realm) in iOS project?

Sorry for my english.
I have developed a mobile application that needs its own local data store. I chose Realm as the database management system. In the process of studying the Realm documentation I had no problems with the database design, it's normalization, the CRUD-operations and everything that is related directly to the code.
I was confused following. I test my application in the simulator. I did not create the local database. I’ve just done it as it is written in the documentation of Realm. Realm created by itself its database and I did not specify any settings. The location of my file with database is given below:
/Users/macbookpro/Library/Developer/CoreSimulator/Devices//EFECD945-285C-494F-8C1B-950D9AA05147/data/Containers/Data/Application/8D71FF0A-60D2-4875-96BB-36955E80D505/Documents/default.realm.
I quite inconvenient to apply each time this path for the analyzing of records in the database. The file is stored at this path is inaccessible for Realm Browser, because I need root privileges. I have seen that people store their .realm-files in project directory. Answer me please - how can I do it? Would it be the right alternative?
I have repeatedly seen the code, where paths to the database file appeared. For example, here: https://github.com/pietbrauer/CarthageRealmUploadFailureExample/blob/master/Carthage.checkout/realm-cocoa/examples/ios/swift/Migration/AppDelegate.swift
I'm not sure if it's the right direction.
As a result, I want to:
1) have on hand a database file and be able to quickly refer to it;
2) know the algorithm - how to set own path to the Realm-database
file;
3) know what realm configuration must be on a real device, to
ensure security and data integrity.
Thank you very much to all!
When you don't specify a path for the default Realm in your app, Realm will automatically create one named default.realm in the Documents directory of your app. On apps running in the iOS Simulator, this will save the Realm file in the appropriate Simulator folder, but you are correct in that it's not very intuitive to find it.
In response to your questions:
There is a cool utility called SimPholders that lets you inspect the Documents folders of apps in the iOS Simulator. This is the best way to get at any Realm files the Simulator has generated very quickly.
When creating an instance of a Realm object, you can supply a Configuration object to customise it. You can explicitly set the file path of the Realm file by setting the path property of that Configuration object.
iOS supplies an automatic layer of file encryption on disk, that makes files unable to be read when the device is locked. If you want additional security, then it's possible to set the encryptionKey property of a Realm Configuration object to have a Realm itself encrypt that Realm file on disk.
I hope that helped! Let me know if you need any more clarification!
1) and 2) does have very small effect on security and data integrity
3) if you are worry about security of you data, use Realm-level encryption like here

Unload Realm because of new database file

I am trying to update precompiled database in my app using a new database file downloaded from the internet. However when I download the file and replace the old one used by Realm with it, Realm still uses the old one till the next app restart. Unfortunately I still need to open the first DB to copy some data from it before downloading a new database. Is there a possiblity to force unload/reload the whole database?
I made a mistake and I accidentally created a new instance of Realm. The problem is that Realm instances are cached. I needed to use autoreleasepool {} and ensure that Realm is created only in the block.

Bind sqlite database with custom iOS framework

I have developed a custom ios framework for achieving some task, which is working perfectly but I have to add my sqlite database separately in my test app. I want to bind my sqlite database with my custom framework so that it will automatically added in any app where user want to user that framework.
I also don't want to show sqlite database to anyone. can anyone please tell me the best way to achieve this.
I believe you're looking to ship a sqlite database with your app, and have it installed invisibly to the user. Just add the database to your NSBundle, by including it in the application. The important part is that you must copy this bundled database to the file system (you probably want to also mark it as not being in the cloud), because the NSBundle'd files are read-only. So, here's your strategy: 1) on startup, check to see if the database is in the file system. 2) if not, copy it from the NSBundle to the file system, 3) open the database in the file system.
Maybe there is a better solution to your question but here is a suggestion on what you could: Programmatically create the database in your framework. As for hiding the database here is the solution to that! You may also encrypt the sqlite file.
Cheers

What is the appropriate extention for creating database in iOS?

I am creating a database application using the SQLite3 library.
I have created a database file using each of the file extensions: .db, .sqlite, and .sql.
All are working fine for me, but my question is which extension should I use in general?
Is there any difference between these file extentions?
The Sqlite documentation seems to use the '.db' extension consistently, but I've seen plenty of Sqlite files that use '.sqlite' instead. Use whatever is meaningful to you. For example, if you're using Core Data to create the database, you might use '.cd' or .'coredata' to remind yourself not to modify the database outside of Core Data. Unless you're planning to transfer the file to some other machine (and really, even then) it won't matter.
The database will live in your application's sandbox, so users will never have to know about the filename or the extension, and other applications typically won't ever see it either. Just give it a distinct name so you can tell it apart from other files that your app might be saving to the same location.

Reverse-engineering Core Data db results in "Can't find model for source store" error

I have the task to re-engineer an iPhone app which makes use of Core Data to store some values. Unfortunately I do not have access to the original source code but I do have acces to old database files, copied directly from the device where the old version of the app is installed.
I have to create a new version of the app with some new functionality and I am trying to import the data from the Sqlite Db and migrate it to a new version.
I am already failing at the task to read the data from the old database. Though I can open the Sqlite file and such was able to exactly reproduce the data structure in my own datamodel, everytime I try to read the data, it fails with a
Can't find model for source store
error.
Ignoring the old data is not an option because there is important data stored there.
I googled for solutions and tried every recommended solution related to migrating data but it all fails. Maybe I can not use the Sqlite created by another app? Or so I overlook something in re-engineering the structure of the datamodel even when I used exactly the some field names and datatypes? Is there another way around this?
I could not solve the issue but I found a workaround. By using Sqlite directly, I was able to query the legacy data and import it into my newly created data model. A good starter point for using Sqlite is here:
http://www.techotopia.com/index.php/IOS_4_iPhone_Database_Implementation_using_SQLite

Resources