This question is about using two different ways to insert objects into Realm. I noticed that the first method is much faster, but the size result is huge compared to the second method. The difference between the two approaches is to move write the transaction outside vs inside the for loop.
// Create realm file let realm = try! Realm(fileURL: banco_url!)
When I add such objects, the Realm file grows to 75.5MB:
try! realm.write { for i in 1...40000 { let new_realm_obj = realm_obj(value: ["id" : incrementID(), "a": "123", "b": 12.12, "c": 66, "d": 13.13, "e": 0.6, "f": "01100110", "g": DateTime, "h": 3]) realm.add(new_realm_obj) print("๐น \(i) Added") } }
When I add such objects, the Realm file grows to 5.5 MB:
for i in 1...40000 { let new_realm_obj = realm_obj(value: ["id" : incrementID(), "a": "123", "b": 12.12, "c": 66, "d": 13.13, "e": 0.6, "f": "01100110", "g": DateTime, "h": 3]) try! realm.write { realm.add(new_realm_obj) print("๐น \(i) Added") } }
My class to add to the area file
class realm_obj: Object { dynamic var id = Int() dynamic var a = "" dynamic var b = 0.0 dynamic var c = Int8() dynamic var d = 0.0 dynamic var e = 0.0 dynamic var f = "" dynamic var g = Date() dynamic var h = Int8() }
Auto zoom function
func incrementID() -> Int { let realm = try! Realm(fileURL: banco_url!) return (realm.objects(realm_obj.self).max(ofProperty: "id") as Int? ?? 0) + 1 }
Is there a better or right way to do this? Why in these cases do I get such different file sizes?