Reading data from ios to realm and using threads at the same time But "Realm accessed from incorrect thread." An error occurs Is there a problem with your code?
let realm = try! Realm()
let readData = realm.objects(DataRealm.self)
for i in 0...readData.count-1 {
DispatchQueue.global().async {
self.parsing()
}
}
You cannot use Realm objects across threads. That's why you get this error.
But you can use references which you can pass across them. I am not sure how to apply it to your code since there is no part where you use the individual objects from readData . But what you are searching for might be something like this:
let realm = try! Realm()
let readData = realm.objects(DataRealm.self)
for data in readData {
let readDataRef = ThreadSafeReference(to: data)
DispatchQueue.global().async {
self.parsing(readDataReference: readDataRef)
}
}
In your parsing() method you would need to get the object from the reference then:
let data = realm.resolve(readDataReference)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.