This isn't exactly an answer but may provide some validatation and illustrate an interesting point.
TL;DR
Writing chunks of objects over time does not have a significant impact on write times - you should not be seeing a significant increase in write times based on the existing volume of data in this use case.
The long version with setup and testing:
When a write has completed, realm will generate a notification so we're using that notification to 'time' the writes.
We start with a class var to store the start time of each write and a notification to track when it's done
var startTime = Date()
var gameStatNotificationToken: NotificationToken?
then we add an observer to realm that will execute when the write has completed that determines the elapsed time between the start time and now
func addObserver {
if let realm = gGetRealm() { //this just gets a realm
self.gameStatNotificationToken = realm.observe { notification, realm in
let elapsed = Date().timeIntervalSince(self.startTime)
print("(elapsed)")
}
}
}
Then the code to actually write the data
func writeLotsOfData() {
autoreleasepool {
var gameStatArray = [GameStat]()
for index in 0..<10000 {
let stat = GameStat()
stat.playerIden = "(index)"
gameStatArray.append(stat)
}
if let realm = gGetRealm() {
self.startTime = Date()
try! realm.write {
realm.add(gameStatArray)
}
}
}
}
and then a loop that calls the above code 10 times; writing 10k per loop, 100k total objects.
func looper() {
for i in 0..<10 {
self.writeLotsOfData()
}
}
So the end result shows no increase in writing over time
0.19487500190734863
0.1404169797897339
0.14565002918243408
0.15493690967559814
0.14426898956298828
0.15933406352996826
0.15379595756530762
0.16700804233551025
0.1598280668258667
0.15421009063720703
EDIT
Here's the GameStat object I am using - it's identical to the one in the question
class GameStat: Object {
@objc dynamic var iden = UUID().uuidString
@objc dynamic var playerIden = "" // Links to iden of a Player Object
@objc dynamic var statOne = 0
@objc dynamic var statTwo = 0
@objc dynamic var statThree = 0
// Primary Key
override static func primaryKey() -> String? {
return "iden"
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…