How to create a pre-prepared database and then upload this data to my iOS swift app?

I have a lot of text data in multiple text files. How can I effectively create a database (Realm?) With data from these text files so that I can add the database to my Xcode project and load the data into my application?

I saw many tutorials on how to create a Realm database with user-entered data and then load it, but none of them contain pre-created databases. I do not know if Realm is suitable for this program, but I look very good. I downloaded the Realm browser, but I could only browse the databases and could not easily find out how easy it was to create them.

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 people not contain the file data, which I am 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 a class that defines a 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
}

Image of the area file I'm trying to upload: enter image description here

Thanks for the help!

+4
source share
3 answers

Create 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 a 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 database path:

print(Realm.Configuration.defaultConfiguration.fileURL!)

Now follow these steps (explained by scope)

  • Drag the new compressed copy of your Realm file into your final Xcode Project Navigator applications.
  • Xcode Realm " Bundle".
  • Realm . , NSBundle.main.pathForResource(_: ofType:).

:

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
}

:

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)")
}
+3

Realm Browser ( ) - .

,
, "" , "" xCode.

, :

[[NSBundle mainBundle] pathForResource:@"nameOfFile" ofType:@"realm"];

, , ​​ Documents ( )

+1

, , , https://github.com/Ahmed-Ali/JSONExport JSONExportV Mac App Store http://realmgenerator.eu/

If you already have JSON, then they will create Realm models that you can add to your project. JSONExport supports more languages ​​and seems to work better for me with Swift. Just make sure you set the language to "Swift - Realm".

+1
source

All Articles