                     self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
                     return
                 }
-                
-                let space = self.spacesPerId[room.roomId] ?? room.toSpace()
+
+                var space: MXSpace?
+                self.spacesPerIdReadWriteQueue.sync(flags: .barrier) {
+                    space = self.spacesPerId[room.roomId] ?? room.toSpace()
+                }
                 
                 self.prepareData(with: roomIds, index: index, output: output, room: room, space: space, isRoomDirect: room.isDirect, directUserId: room.directUserId, completion: completion)
             }

public class MXSpaceService: NSObject {

    // MARK: - Properties
    
    private unowned let session: MXSession
    
    private lazy var stateEventBuilder: MXRoomInitialStateEventBuilder = {

                    spacesPerId[spaceId] = space
                }
            }
            spacesPerIdReadWriteQueue.sync(flags: .barrier) {
                self.spacesPerId = spacesPerId
            }
        }
    }
    private var spacesPerId: [String:MXSpace] = [:]
