 public class MXSpaceService: NSObject {
 
     // MARK: - Properties
-    
+
+    private let spacesPerIdReadWriteQueue: DispatchQueue = {
+      let label = "org.matrix.sdk.MXSpaceService.spacesPerIdReadWriteQueue"
+      return DispatchQueue(label: label, attributes: .concurrent)
+    }()
+
     private unowned let session: MXSession
     
     private lazy var stateEventBuilder: MXRoomInitialStateEventBuilder = {

                    spacesPerId[spaceId] = space
                }
            }
            self.spacesPerId = spacesPerId
        }
    }
    private var spacesPerId: [String:MXSpace] = [:]

    /// - Parameter spaceId: The id of the space.
    /// - Returns: A MXSpace with the associated roomId or null if room doesn't exists or the room type is not space.
    public func getSpace(withId spaceId: String) -> MXSpace? {
        var space: MXSpace?
        spacesPerIdReadWriteQueue.sync(flags: .barrier) {
           space = self.spacesPerId[spaceId]
        }
        if space == nil, let newSpace = self.session.room(withRoomId: spaceId)?.toSpace() {
            space = newSpace
            spacesPerIdReadWriteQueue.sync(flags: .barrier) {
                self.spacesPerId[spaceId] = newSpace
            }
        }
        return space
    }
