Skip to content

Instantly share code, notes, and snippets.

@theladyjaye
Last active December 29, 2015 19:15
Show Gist options
  • Save theladyjaye/9615730413415d4470d9 to your computer and use it in GitHub Desktop.
Save theladyjaye/9615730413415d4470d9 to your computer and use it in GitHub Desktop.
Realm Primary Key Insert Performance @ 1 million + 1 records
import Foundation
import RealmSwift
class StringObject: Object{
dynamic var id: String = ""
override static func primaryKey() -> String{
return "id"
}
}
class IntObject: Object{
dynamic var id: Int = 0
override static func primaryKey() -> String{
return "id"
}
}
func testIntPerformanceExample() {
let realm = db.realm
let ids = (0..<max).map{ i -> IntObject in
let obj = IntObject()
obj.id = i
return obj
}
try! realm.write {
ids.forEach{ obj in
realm.add(obj)
}
}
let obj = IntObject()
obj.id = max + 1
self.measureBlock {
try! realm.write {
realm.add(obj)
}
}
}
func testUUIDPerformanceInsertPerformance() {
let realm = db.realm
let ids = (0..<max).map{ _ -> StringObject in
let obj = StringObject()
obj.id = NSUUID().UUIDString
return obj
}
try! realm.write {
ids.forEach{ obj in
realm.add(obj)
}
}
let obj = StringObject()
obj.id = NSUUID().UUIDString
self.measureBlock {
try! realm.write {
realm.add(obj)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment