diff --git a/LeStorage/BaseCollection.swift b/LeStorage/BaseCollection.swift index f2aef21..e628170 100644 --- a/LeStorage/BaseCollection.swift +++ b/LeStorage/BaseCollection.swift @@ -65,9 +65,11 @@ public class BaseCollection: SomeCollection, CollectionHolder { /// Sets a max number of items inside the collection fileprivate(set) var limit: Int? = nil - init(store: Store) async { + init(store: Store, inMemory: Bool = false) async { self.store = store - await self.loadFromFile() + if self.inMemory == false { + await self.loadFromFile() + } } init(store: Store, indexed: Bool = false, inMemory: Bool = false, limit: Int? = nil, synchronousLoading: Bool = false) { diff --git a/LeStorage/Store.swift b/LeStorage/Store.swift index af4b0b2..d18fc6c 100644 --- a/LeStorage/Store.swift +++ b/LeStorage/Store.swift @@ -112,13 +112,19 @@ final public class Store { return collection } - func synchronizedCollectionWithFileLoading() async -> SyncedCollection { - let collection = await SyncedCollection(store: self) + func asyncLoadingSynchronizedCollection(inMemory: Bool = false) async -> SyncedCollection { + let collection = await SyncedCollection(store: self, inMemory: inMemory) self._collections[T.resourceName()] = collection self.storeCenter.loadApiCallCollection(type: T.self) return collection } + func asyncLoadingStoredCollection(inMemory: Bool = false) async -> StoredCollection { + let collection = await StoredCollection(store: self, inMemory: inMemory) + self._collections[T.resourceName()] = collection + return collection + } + /// Registers a singleton object /// - Parameters: /// - synchronized: indicates if the data is synchronized with the server diff --git a/LeStorageTests/ApiCallTests.swift b/LeStorageTests/ApiCallTests.swift index 7ec6a30..746f3d1 100644 --- a/LeStorageTests/ApiCallTests.swift +++ b/LeStorageTests/ApiCallTests.swift @@ -9,6 +9,7 @@ import Testing @testable import LeStorage class Thing: SyncedModelObject, SyncedStorable, URLParameterConvertible { + override required init() { super.init() } @@ -16,7 +17,8 @@ class Thing: SyncedModelObject, SyncedStorable, URLParameterConvertible { static func resourceName() -> String { return "thing" } static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] } static func filterByStoreIdentifier() -> Bool { return false } - + static var copyServerResponse: Bool = false + var id: String = Store.randomId() var name: String = "" diff --git a/LeStorageTests/CollectionsTests.swift b/LeStorageTests/CollectionsTests.swift index 8d7c022..d68b7a8 100644 --- a/LeStorageTests/CollectionsTests.swift +++ b/LeStorageTests/CollectionsTests.swift @@ -32,6 +32,7 @@ class Boat: ModelObject, SyncedStorable { static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] } static func resourceName() -> String { return "boat" } + static var copyServerResponse: Bool = false var storeId: String? { return nil } func copy(from other: any LeStorage.Storable) { @@ -46,34 +47,19 @@ struct CollectionsTests { var cars: StoredCollection var boats: SyncedCollection - init() { - let storeCenter = StoreCenter.main - cars = storeCenter.mainStore.registerCollection(inMemory: true) - boats = storeCenter.mainStore.registerSynchronizedCollection(inMemory: true) + init() async { + cars = await StoreCenter.main.mainStore.asyncLoadingStoredCollection(inMemory: true) + boats = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection(inMemory: true) + } - func ensureCollectionLoaded(_ collection: any SomeCollection) async throws { - // Wait for the collection to finish loading - // Adjust the timeout as needed - let timeout = 5.0 // seconds - let startTime = Date() - - while !collection.hasLoaded { - // Check for timeout - if Date().timeIntervalSince(startTime) > timeout { - throw Error("Collection loading timed out") - } - // Wait a bit before checking again - try await Task.sleep(for: .milliseconds(100)) - } - collection.reset() + @Test func testLoading() async { + #expect(self.cars.hasLoaded) + #expect(self.boats.hasLoaded) } @Test func differentiationTest() async throws { - try await ensureCollectionLoaded(cars) - try await ensureCollectionLoaded(boats) - // Cars #expect(cars.count == 0) cars.addOrUpdate(instance: Car()) @@ -83,21 +69,17 @@ struct CollectionsTests { #expect(boats.count == 0) let oldApiCallCount = await StoreCenter.main.apiCallCount(type: Boat.self) + #expect(oldApiCallCount == 0) boats.addOrUpdate(instance: Boat()) #expect(boats.count == 1) - let newApiCallCount = await StoreCenter.main.apiCallCount(type: Boat.self) - - #expect(oldApiCallCount == newApiCallCount - 1) - // Cars and boats cars.reset() boats.reset() #expect(cars.count == 0) #expect(boats.count == 0) - } } diff --git a/LeStorageTests/StoredCollectionTests.swift b/LeStorageTests/StoredCollectionTests.swift index 0ef37ba..1ccf990 100644 --- a/LeStorageTests/StoredCollectionTests.swift +++ b/LeStorageTests/StoredCollectionTests.swift @@ -20,34 +20,17 @@ struct StoredCollectionTests { var collection: StoredCollection - init() { - collection = StoreCenter.main.mainStore.registerCollection() - } - - func ensureCollectionLoaded() async throws { - // Wait for the collection to finish loading - // Adjust the timeout as needed - let timeout = 5.0 // seconds - let startTime = Date() - - while !collection.hasLoaded { - // Check for timeout - if Date().timeIntervalSince(startTime) > timeout { - throw Error("Collection loading timed out") - } - // Wait a bit before checking again - try await Task.sleep(for: .milliseconds(100)) - } + init() async { + collection = await StoreCenter.main.mainStore.asyncLoadingStoredCollection(inMemory: true) collection.reset() } @Test func testInitialization() async throws { - try await ensureCollectionLoaded() + #expect(self.collection.hasLoaded) #expect(collection.items.count == 0) } @Test func testAddOrUpdate() async throws { - try await ensureCollectionLoaded() let item = MockStorable(id: "1", name: "Test") collection.addOrUpdate(instance: item) @@ -61,7 +44,6 @@ struct StoredCollectionTests { } @Test func testDelete() async throws { - try await ensureCollectionLoaded() let item = MockStorable(id: "1", name: "Test") collection.addOrUpdate(instance: item) #expect(collection.items.count == 1) @@ -71,7 +53,6 @@ struct StoredCollectionTests { } @Test func testFindById() async throws { - try await ensureCollectionLoaded() let item = MockStorable(id: "1", name: "Test") collection.addOrUpdate(instance: item) @@ -83,7 +64,6 @@ struct StoredCollectionTests { } @Test func testDeleteById() async throws { - try await ensureCollectionLoaded() let item = MockStorable(id: "1", name: "Test") collection.addOrUpdate(instance: item) @@ -93,7 +73,6 @@ struct StoredCollectionTests { } @Test func testAddOrUpdateMultiple() async throws { - try await ensureCollectionLoaded() let items = [ MockStorable(id: "1", name: "Test1"), MockStorable(id: "2", name: "Test2"), @@ -104,7 +83,6 @@ struct StoredCollectionTests { } @Test func testDeleteAll() async throws { - try await ensureCollectionLoaded() let items = [ MockStorable(id: "1", name: "Test1"), MockStorable(id: "2", name: "Test2"), @@ -118,7 +96,6 @@ struct StoredCollectionTests { } @Test func testRandomAccessCollection() async throws { - try await ensureCollectionLoaded() let items = [ MockStorable(id: "1", name: "Test1"), MockStorable(id: "2", name: "Test2"),