Realm accessed from incorrect thread

1,145 views
Skip to first unread message

Ivan Schuetz

unread,
Jul 14, 2015, 6:03:15 PM7/14/15
to realm...@googlegroups.com

Hi,


I searched for this and apparently this is error is caused when creating the Realm instance in a different queue than where it's used.


But I'm creating and using the Realm instance in the same place. Don't know what's wrong.


This is my load method:


private func load<T: Object>(handler: [T] -> ()) {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        var results: [T] = Realm().objects(T).toArray()

        dispatch_async(dispatch_get_main_queue(), {

            handler(results)

        })

    })

}


toArray() is implemented like this:


extension Results {

    func toArray() -> [T] {

        return map(self){$0}

    }

}


Confirmed that the error comes from this method, when I comment the GCD code it works. How do I fix it?


Thanks,

Ivan

Ivan Schuetz

unread,
Jul 14, 2015, 6:11:22 PM7/14/15
to realm...@googlegroups.com
Do the realm objects themselves also trigger a read somehow?

Ivan Schuetz

unread,
Jul 14, 2015, 6:34:43 PM7/14/15
to realm...@googlegroups.com
Ah, yes, it seems I also can't pass the realm objects between threads https://github.com/realm/realm-cocoa/issues/1445

I have a model layer but didn't want to couple it with the database and do the mapping outside, but it seems it's suitable to do it in the background thread.

Samuel Giddins

unread,
Jul 14, 2015, 6:54:17 PM7/14/15
to Ivan Schuetz, realm...@googlegroups.com
Ivan,
Yup, you're correct! The issue there is indeed using the objects in the array on the different queue, but we're working on a handy solution to that, and you can follow https://github.com/realm/realm-cocoa/issues/1097 for further updates on that!

--
Samuel Giddins



--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/ba7c86b2-116c-45a9-8b25-fe551e57509d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:104665238-1484#}

Ivan Schuetz

unread,
Jul 14, 2015, 9:40:03 PM7/14/15
to realm...@googlegroups.com, he...@realm.io
Cool that it's being solved!

For now I came up with this generic solution, which maps the results objects to model objects:

func load<T: Object, U>(mapper: T -> U, filter filterMaybe: String? = nil, handler: [U] -> ()) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        var results = Realm().objects(T)
        if let filter = filterMaybe {
            results = results.filter(filter)
        }
             
        let objs: [T] = Realm().objects(T).toArray()
        let models = objs.map{mapper($0)}
             
        dispatch_async(dispatch_get_main_queue(), {
            handler(models)
        })
    })
Reply all
Reply to author
Forward
0 new messages