|
|
|
|
@ -71,7 +71,9 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
/// Indicates if the collection has loaded locally, with or without a file |
|
|
|
|
fileprivate(set) public var hasLoaded: Bool = false |
|
|
|
|
|
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true) { |
|
|
|
|
fileprivate(set) var limit: Int? = nil |
|
|
|
|
|
|
|
|
|
init(synchronized: Bool, store: Store, indexed: Bool = false, asynchronousIO: Bool = true, inMemory: Bool = false, sendsUpdate: Bool = true, limit: Int? = nil) { |
|
|
|
|
self.synchronized = synchronized |
|
|
|
|
self.asynchronousIO = asynchronousIO |
|
|
|
|
if indexed { |
|
|
|
|
@ -80,6 +82,7 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self._inMemory = inMemory |
|
|
|
|
self._sendsUpdate = sendsUpdate |
|
|
|
|
self._store = store |
|
|
|
|
self.limit = limit |
|
|
|
|
|
|
|
|
|
self._load() |
|
|
|
|
} |
|
|
|
|
@ -222,11 +225,18 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
} else { // insert |
|
|
|
|
self.items.append(instance) |
|
|
|
|
self._sendInsertionIfNecessary(instance) |
|
|
|
|
self._applyLimitIfPresent() |
|
|
|
|
} |
|
|
|
|
self._indexes?[instance.id] = instance |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _applyLimitIfPresent() { |
|
|
|
|
if let limit { |
|
|
|
|
self.items = self.items.suffix(limit) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sends a POST request for the instance, and changes the collection to perform a write |
|
|
|
|
public func writeChangeAndInsertOnServer(instance: T) { |
|
|
|
|
defer { |
|
|
|
|
@ -308,6 +318,8 @@ public class StoredCollection<T: Storable>: RandomAccessCollection, SomeCollecti |
|
|
|
|
self._indexes?[instance.id] = instance |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self._applyLimitIfPresent() |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Returns the instance corresponding to the provided [id] |
|
|
|
|
|