Introducing Realm Cocoa Converter
Today, we’re launching a new open source library to complement Realm Objective-C and Realm Swift called Realm Cocoa Converter!
We’ve heard from many of our users that there are times in their development where they would like to be able to move large batches of data in and out of Realm files more easily. The goal of Realm Cocoa Converter is to provide a common library to enable importing data from other file formats into Realm, and vice-versa, exporting data from Realm.
The library is written in Swift, and is very easy to work with. For exporting a Realm file, it only takes a single class. For example, exporting to CSV:
let path = ... // Absolute path to the target Realm file let destinationFolder = ... // Absolute path to the folder in which to save the CSV files let exporter = CSVDataImporter(realmFilePath: path) exporter.exportToFolderAtPath(outputFolderPath: destinationFolder)
When importing files into Realm, the process is a little more complicated. This is because Realm needs to generate a schema for each type of object in the input file before the data can be imported.
Realm Cocoa Converter handles this by having a separate class for intelligently generating the schema, which can then be either saved, or modified by the user before the import.
This schema is then supplied when the import operation takes place.
var filePaths = [String]() // An array of paths to each file representing a Realm table // Create an instance of the schema generator and analyze the contents of those data files let generator = ImportSchemaGenerator(files: filePaths) let schema = try! generator.generate() let destinationRealmPath = ... // The destination folder for this Realm file // Create an importer object, and import those files using the previously generated schema let dataImporter = CSVDataImporter(files: filePaths) try! dataImporter.importToPath(String(destinationRealmPath), schema: schema)
While Realm Cocoa Converter itself is implemented in Swift, it has also been designed to properly integrate with Objective-C and its code conventions as well.
NSString *path = ... // Absolute path to the target Realm file NSString *destinationFolder = ... // Absolute path to the folder in which to save the CSV files RLMCSVDataExporter *exporter = [[RLMCSVDataExporter alloc] initWithRealmFileAtPath:realmFilePath]; [exporter exportToFolderAtPath:destinationFolder withError:nil];
Currently, the library contains support for generating Realm files from groups of CSV and XLSX files, and the ability to export to CSV files. We’re planning on adding more formats in the future, including JSON and SQLite.
Realm Cocoa Converter is still very much in its infancy, and we’d love to have you help in its development! If you have any specific use-cases in your app for it, or additional formats you think it should support, please let us know!