我有一个50k文件的列表,我需要将它保存到领域。
我以下面的方式保存。在此操作之前,域文件是40KB。在此之后它增加到200 MB。你能告诉我一个最好的方法或我的代码有什么问题。我创建了一个保存领域文件列表的函数。
func saveList<T: Object>(_ list: List<T>?) {
autoreleasepool {
if let list = list {
let backgroundQueue = DispatchQueue(label: "realm", qos: .background, attributes: [], autoreleaseFrequency: .workItem, target: nil)
var defaultRealm: Realm?
backgroundQueue.sync {
autoreleasepool {
do {
defaultRealm = try Realm(configuration: Realm.Configuration(fileURL:URL(path)))
} catch let error {
debugPrint("Realm Can't be created on thread \(Thread.current) and error is \(error.localizedDescription)")
}
if let realm = defaultRealm {
defer {
debugPrint("All Saving done. - May be success or failure")
}
do {
try realm.write {
for realmObject in list {
autoreleasepool{
realm.add(realmObject, update: true)
}
}
}
} catch let error {
debugPrint(" Can't add Realm Object on thread \(Thread.current) thread and error is \(error.localizedDescription)")
}
}
}
}
}
}
}
请告诉我这是否有任何问题。