Skip to content

Commit

Permalink
fix: Harden InMemoryFileListViewModel use of frozen realm object
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-coye committed Feb 19, 2024
1 parent 8ef1713 commit 4f1d3e8
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ class InMemoryFileListViewModel: FileListViewModel {
private let realm: Realm

override init(configuration: Configuration, driveFileManager: DriveFileManager, currentDirectory: File) {
if let realm = currentDirectory.realm {
// TODO: Refactor to explicit realm state
/// We expect the object to be live in this view controller, if not detached.
var currentDirectory = currentDirectory
if currentDirectory.isFrozen, let liveDirectory = currentDirectory.thaw() {
currentDirectory = liveDirectory
}

if let realm = currentDirectory.realm, !currentDirectory.isFrozen {
self.realm = realm
} else {
let unCachedRealmConfiguration = Realm.Configuration(
Expand All @@ -40,6 +47,7 @@ class InMemoryFileListViewModel: FileListViewModel {
}

super.init(configuration: configuration, driveFileManager: driveFileManager, currentDirectory: currentDirectory)

try? realm.write {
realm.add(currentDirectory)
}
Expand Down

0 comments on commit 4f1d3e8

Please sign in to comment.