I have a lot of data text in a couple of text files. How can I efficiently create a (Realm?) database with the data from those text files so I can add the database to my Xcode project and load the data into my app?
I have seen many tutorials on how to create Realm database with user entered data and then load it, but none with pre-made databases. I don't know if Realm is the right program to do this, but I looked really well. I have downloaded the Realm Browser, but I could only view databases and couldn't find out how to create them easily.
EDIT:
I managed to create a database in Realm and put it in my xcode folder. Then I try to load it like this, but let peoples doesn't contain the file's data, what am I missing:
let path = NSBundle.mainBundle().pathForResource("data", ofType: "realm")
var config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: path!))
config.readOnly = true
let realm = try! Realm(configuration: config)
let peoples = realm.objects(Data)
Data is class which defines the schema:
class Data : Object {
dynamic var name = ""
dynamic var country = ""
dynamic var discription = ""
dynamic var image = ""
dynamic var cartoon = ""
dynamic var startYear = 0
dynamic var endYear = 0
}
An image of the realm file I'm trying to load:
Thanks for help!
Create a Sample Model:
final class ContentModel: Object {
dynamic var title = ""
dynamic var content = ""
dynamic var listName = ""
dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
}
Create realm database:
let model = ContentModel()
model.id = 1
model.listName = "List Item 1"
model.title = "Title of content 1"
model.content = "Sample Text"
// Get the default Realm
let realm = try! Realm()
// Persist your data easily
try! realm.write {
realm.add(model)
}
Use this line to print the path to database:
print(Realm.Configuration.defaultConfiguration.fileURL!)
Now follow these steps (explained by realm)
Here's the code to get your bundled resource:
open class func getBundledRealm() -> Realm {
let config = Realm.Configuration(
// Get the URL to the bundled file
fileURL: Bundle.main.url(forResource: "default", withExtension: "realm"),
// Open the file in read-only mode as application bundles are not writeable
readOnly: true)
// Open the Realm with the configuration
let realm = try! Realm(configuration: config)
return realm
}
To Test your database:
let realm = RealmUtils.getBundledRealm()
// Read some data from the bundled Realm
let results = realm.objects(ContentModel.self)
for item in results {
print("Id: \(item.id)")
}
Realm Browser (as the name said) is just a browser.
for create pre-prepared database you should write some codes,
create empty database and insert data to it, then save it to Documents folder of simulator
, comment insert code and copy generetated database from Documents folder, and add it as resource to your xCode project.
if your database static and you don't want to change anything on it simply just load your database with resource bundle path:
[[NSBundle mainBundle] pathForResource:@"nameOfFile" ofType:@"realm"];
but if you want to change some data you must copy it again to writable folder like Documents (for first time only)
I think what you are looking for is something like https://github.com/Ahmed-Ali/JSONExport or JSONExportV in the Mac App Store or http://realmgenerator.eu/
If you already have your JSON, then these will generate the Realm Models that you can drop into your project. JSONExport supports way more languages and seems to work better for me with Swift. Just make sure you set the language to "Swift - Realm".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With