How to ensure that some code runs on same background thread with runloop


#1

I am using realm in an iOS app.

I am calling realm methods in a background thread. Realm objects can be used only in the thread where it was created.

This means that I have to ensure that all the code related to accessing realm objects happen on the same thread.

The background thread has no runloop and hence realm objects do not get refreshed automatically.

Can any one point out how to create a background thread with a runloop so that the code blocks can be executed on the same background thread with runloop?

PS. I have asked the same question at StackOverflow


#2

In my architect, this is not a problem.
Basically, I’m mapping all of my realm object to a struct and use that.
When I do the mapping, I change the thread to - the realm has a dedicated thread.

Although I’m not using any notification, but I’m sure that can be fixed too somehow :slight_smile:


#3

Thanks for replying.

You mentioned that the realm has a dedicated thread.

The way you create and use that dedicated thread for realm - is it similar to the this answer. If it is not similar, can you share how you have created the dedicated thread


#4
public func createUserWith(email: String,
                               password: String,
                               realmIdentity: String,
                               fbId: String,
                               callback: @escaping (Result<RealmUser, NSError>) -> Void) {
        DispatchQueue(label: "realm").async {
            autoreleasepool {
                if let realm = try? Realm() {
                    let realmUser = RealmUser()
                        realmUser.email = email
                        realmUser.password = password
                        realmUser.platform = "ios"
                        realmUser.realmIdentity = realmIdentity
                        realmUser.facebookUserId = fbId
                    do {
                        try realm.write {
                            realm.add(realmUser)
                            callback(.success(realmUser))
                        }
                    } catch {
                        callback(.failure(error as NSError))
                    }
                } else {
                    callback(
                        .failure(
                            NSError(domain: "",
                                    code: 0,
                                    userInfo: [NSLocalizedDescriptionKey: "RealmUserFactory.Realm doesnt exist"])))
                }
            }
        }
    }

This is the actual createUser function.


#5

Upon reading a bit more in this theme, my solution doesn’t necessarily work all the time [like it explained in that StackOverflow thread].

All the other solution saves the dispatchqueue to a variable -> thats better for performance, but I think thats why they have issues with it - although I’m not sure.
Also, my solution is in the realm docs too, but now I’m not that confident that it works 100%…