Realm uses copy-on-write semantics whenever changes are performed in write transactions.
The larger the structure that has to be forked & copied, the longer it will take to perform the operation.
This small unscientific benchmark on my 2.8GHz i7 MacBook Pro
import Foundation
import RealmSwift
class Model: Object {
dynamic var prop1 = 0.0
dynamic var prop2 = 0.0
}
// Add 60 million objects with two Double properties in batches of 10 million
autoreleasepool {
for _ in 0..<6 {
let start = NSDate()
let realm = try! Realm()
try! realm.write {
for _ in 0..<10_000_000 {
realm.add(Model())
}
}
print(realm.objects(Model.self).count)
print("took \(-start.timeIntervalSinceNow)s")
}
}
// Add one item to that Realm
autoreleasepool {
let start = NSDate()
let realm = try! Realm()
try! realm.write {
realm.add(Model())
}
print(realm.objects(Model.self).count)
print("took \(-start.timeIntervalSinceNow)s")
}
Logs the following:
10000000
took 25.6072470545769s
20000000
took 23.7239990234375s
30000000
took 24.4556020498276s
40000000
took 23.9790390133858s
50000000
took 24.5923230051994s
60000000
took 24.2157150506973s
60000001
took 0.0106720328330994s
So you can see that adding many objects to the Realm, with no relationships, is quite fast and stays linearly proportional to the number of objects being added.
So it's likely that you're doing more than just adding objects to the Realm, maybe you're updating existing objects, causing them to be copied?
If you're reading a value from all objects as part of your write transactions, that will also grow proportionally to the number of objects.
Avoiding these things will shorten your write transactions.