Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pre-prepared database and then load that data in my ios swift app?

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: enter image description here

Thanks for help!

like image 414
Eric Avatar asked May 02 '16 16:05

Eric


3 Answers

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)

  1. Drag the new compacted copy of your Realm file to your final app’s Xcode Project Navigator.
  2. Go to your app target’s build phases tab in Xcode and add the Realm file to the “Copy Bundle Resources” build phase.
  3. At this point, your bundled Realm file will be accessible to your app. You can find its path by using NSBundle.main.pathForResource(_:ofType:).

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)")
}
like image 115
Noor Ali Butt Avatar answered Oct 11 '22 09:10

Noor Ali Butt


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)

like image 28
Amir Khorsandi Avatar answered Oct 11 '22 08:10

Amir Khorsandi


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".

like image 21
matt.writes.code Avatar answered Oct 11 '22 10:10

matt.writes.code