Colleagues, now I develop an app of bus schedule and before I used sqlite. Size of DB was 585kb and it's really cool. I decided to use Realm and after json parsing I see size - 9,6Mb (more than about 20 times). Please, tell me why? It's not a problem for me, but it's interesting!
example of code:
self.realm.beginWrite()
let file = Bundle.main.path(forResource: "file", ofType: "json")!
let url = URL(fileURLWithPath: file)
let jsonData = NSData(contentsOf: url)!
let json = try! JSONSerialization.jsonObject(with: jsonData as Data, options: [])
self.realm.create(DataRoot.self, value: json, update: true)
try! self.realm.commitWrite()
Realm and SQLite are built (and operate) in very different ways.
One feature of Realm is to allow for lock-free concurrent access of multiple versions of the database, from different threads. This provides a level of realtime access to the database and isolation that's not possible with a single-point extraction/insertion database API like SQLite provides.
To allow different threads to access different versions of the database concurrently, these different "snapshots" are stored in Realm following a copy-on-write scheme. As soon as Realm can detect that an older version is no longer being accessed, it can mark that portion of the copy-on-write tree as "unused". However, to prevent over-allocation, the file on the file system isn't truncated at this point, so new data that's written to the Realm can be done much more quickly.
The nice thing about the way this feature is implemented is that if you don't want to use it (and access Realm like you would SQLite), there's no overhead to storing different versions, and the Realm file can be much smaller than the equivalent data stored in SQLite.
You can read more about this in Realm's docs.
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