Perhaps I am not fully understanding. Let me share my testing code:
For simplicity I have Thing object that just has default values based on your JSON
class Thing: Object {
@Persisted var drugId = 1713
@Persisted var ifile = "LLY07151"
@Persisted var color = "white"
@Persisted var shape = ""
@Persisted var score = ""
@Persisted var clarity = "cloudy"
@Persisted var coating = ""
@Persisted var subshape = ""
@Persisted var subcolor = "milky white"
@Persisted var flavor = ""
@Persisted var imprint1 = ""
@Persisted var imprint2 = ""
@Persisted var formulation = "human recombinant 70 units-30 units/mL"
@Persisted var drugForm = "suspension"
@Persisted var genericName = "insulin isophane-insulin regular"
}
I then have a class var that stores the start time so we can determine the write time
var startTime = Date()
and then a function that creates 10,000 Thing objects and stores them in an array, once completed the startTime is populated and then that array is written to Realm. When the write completes, the endTime is calculated and printed to console.
func writeABunchOfThings() {
let realm = //your realm
var myThingArray = [Thing]()
for _ in 0...9999 { // <-10,000 objects
let myThing = Thing()
myThingArray.append(myThing)
}
self.startTime = Date()
realm.writeAsync {
realm.add(myThingArray)
} onComplete: { error in
let elapsed = Date().timeIntervalSince(self.startTime)
print("done inserting. Elapsed time: \(elapsed)")
}
}
If I run that code three times, deleting the Realm file in between runs, I get this output in console
done inserting. Elapsed time: 0.5658090114593506
done inserting. Elapsed time: 0.5397540330886841
done inserting. Elapsed time: 0.5507090091705322
I believe the issue with your code is writing each object separately within a tight loop on the main thread- if you need that functionality (which may be more memory safe) this would work noting it’s an asynchronous write.
self.startTime = Date()
realm.writeAsync {
for _ in 0...9999 {
realm.create(Thing.self)
}
} onComplete: { error in
let elapsed = Date().timeIntervalSince(self.startTime)
print("done inserting. Elapsed time: \(elapsed)")
}
and the output
done inserting. Elapsed time: 0.9166730642318726
done inserting. Elapsed time: 0.9176139831542969
done inserting. Elapsed time: 0.911078929901123
So not quite as fast but still within reason.
Let me know if that helps.
Jay