-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extension for observing realm object changes and replace objectWi…
…llChange with extension
- Loading branch information
1 parent
04563c0
commit aa566ff
Showing
15 changed files
with
134 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
godtools/App/Share/Data/RealmDatabase/RealmDatabase+ObserveChanges.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// RealmDatabase+ObserveChanges.swift | ||
// godtools | ||
// | ||
// Created by Levi Eggert on 11/8/24. | ||
// Copyright © 2024 Cru. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import RealmSwift | ||
|
||
extension RealmDatabase { | ||
|
||
func observeCollectionChangesPublisher(objectClass: Object.Type, prepend: Bool) -> AnyPublisher<Void, Never> { | ||
|
||
if prepend { | ||
|
||
return openRealm() | ||
.objects(objectClass.self) | ||
.objectWillChange | ||
.prepend(Void()) | ||
.eraseToAnyPublisher() | ||
} | ||
else { | ||
|
||
return openRealm() | ||
.objects(objectClass.self) | ||
.objectWillChange | ||
.eraseToAnyPublisher() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
godtoolsTests/App/Share/Data/RealmDatabase/RealmDatabaseObserveChangesTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// RealmDatabaseObserveChangesTests.swift | ||
// godtools | ||
// | ||
// Created by Levi Eggert on 11/8/24. | ||
// Copyright © 2024 Cru. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
@testable import godtools | ||
import RealmSwift | ||
import Combine | ||
|
||
class RealmDatabaseObserveChangesTests: XCTestCase { | ||
|
||
private let realmDatabase: RealmDatabase = RealmDatabase( | ||
databaseConfiguration: RealmDatabaseConfiguration( | ||
cacheType: .disk( | ||
fileName: "RealmDatabaseObserveChangesTests", | ||
migrationBlock: {migration,oldSchemaVersion in }), | ||
schemaVersion: 1 | ||
) | ||
) | ||
|
||
private var cancellables: Set<AnyCancellable> = Set() | ||
|
||
override func setUp() { | ||
// Put setup code here. This method is called before the invocation of each test method in the class. | ||
} | ||
|
||
override func tearDown() { | ||
// Put teardown code here. This method is called after the invocation of each test method in the class. | ||
} | ||
|
||
func testObserveChangesFires() { | ||
|
||
let expectation = expectation(description: "") | ||
|
||
var didReceiveValue: Bool = false | ||
|
||
realmDatabase | ||
.observeCollectionChangesPublisher(objectClass: TestRealmObject.self, prepend: false) | ||
.receive(on: DispatchQueue.main) | ||
.sink { _ in | ||
|
||
} receiveValue: { _ in | ||
|
||
if !didReceiveValue { | ||
|
||
didReceiveValue = true | ||
|
||
expectation.fulfill() | ||
} | ||
} | ||
.store(in: &cancellables) | ||
|
||
wait(for: [expectation], timeout: 5) | ||
|
||
XCTAssertTrue(didReceiveValue) | ||
} | ||
} |