Search code examples
iosswiftdatabasecsvrealm

Get csv file in realm database with Swift


I have a csv file which looks like this

Tag1,Tag2,Tag3,
some1,some2,some3,
some4,some5,some6,

is there a way to import this file to the realm database programatically?

Like everytime the app starts the new csv file should be imported in the realm database.

The Tag1,Tag2,Tag3, should be the column headers and the some... should be the values


Solution

  • There is no out-of-the box solution at the moment for programatically importing contents of a csv file to an existing realm file.

    You can use Realm Studio to create a new realm file from a csv file. Realm Cocoa Converter can also be used to generate a new realm file from one or more csv files, however, to be able to import files into an already existing realm file, you'd need to modify the converter. Bear in mind that Realm Cocoa Converter only support macOS, it cannot be used in an iOS project.

    Or you can write your own code for converting a csv file to Realm Objects. Parsing a CSV file is quite easy, validating it to make sure it can be converted to Realm objects is a bit more complicated. However, from JSON/plist you could automatically parse Realm objects using Codable.