Closed borkasm closed 8 years ago
There is not a one-to-one mapping from dispatch queue to thread. Two blocks that are invoked on a single dispatch queue can be, and often are, executed on different threads. Realm requires that objects be accessed only from the thread they are created, which means that it's not safe to share them between separate blocks dispatched to a given queue. The suggested pattern is to re-fetch your model objects using their primary key in each block that is dispatched.
Thanks for fast reply. I've already thought of that and indeed I used the pattern you suggest. Will such re-fetching in every block impact on performance?
I think my previous Core Data experience caused this confusion as its contexts are tied to queues but not physical threads.
Refetching based on primary key is cheap, and is unlikely to be a performance problem.
Goals
I'm facing unexpected exception in a simple case with @Alamofire (see code below)
Description
I want to fetch data for some entity that I want to update in my database. First, I get an instance by its primary key on the db serial queue, then I call the
fetch()
method for this instance. Response is getting handled on the same queue but when I try to accessself.html
I get the 'Realm accessed from incorrect thread' exception.Code Sample
Version of Realm and Tooling
ProductName: Mac OS X ProductVersion: 10.11.4 BuildVersion: 15E65
/Applications/Xcode.app/Contents/Developer Xcode 7.3 Build version 7D175
~/.gem/ruby/2.0.0/bin/pod 0.39.0 Realm (0.98.6) RealmSwift (0.98.6)
/bin/bash GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)
(not in use here)
/usr/bin/git git version 2.6.4 (Apple Git-63)