Swift Realm Thread Safety Class
Swift Realm Thread Safety Class
I attempted to create a realm instance
with my own thread class and want to use it all the time within this thread, but when I access it for the second time, it usually crashed by Incorrect Thread
and rarely no crash.
realm instance
Incorrect Thread
final class RealmThreadSafe: Thread
typealias Block = () -> Void
static let instance = RealmThreadSafe()
private(set) var realm: Realm!
private(set) var queue = [Block]()
init(start: Bool = true, queue: [Block]? = nil)
super.init()
if let queue = queue
for block in queue
enqueue(block: block)
if start self.start()
name = "RealmThreadSafe"
override func main()
if realm == nil realm = try! Realm()
while true
if isCancelled return
if queue.count > 0
let block = queue.removeFirst()
block()
func enqueue(block: @escaping Block)
queue.append(block)
Any idea for this one ?
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.