Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/mergesort/Boutique. ✨ A magical persistence library (and so much more) for state-driven iOS and Mac apps ✨ build.ms/boutique/docs
actors appkit cache database ios macos offline-first persistence swift swiftui uikit
Something went wrong. Try again.
1.4 kB · 48 lines
Swift
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import Boutiqueimport SwiftUI
@Observablefinal class RichNotesController { @ObservationIgnored @Stored(in: .notesStore) var notes: [RichNote]
init(store: Store<RichNote>) { self._notes = Stored(in: store) }
func addItems(count: Int) async throws { do { var items = Array(repeating: RichNote.demoNote, count: count)
// Adding N items by setting their id to UUIDs to ensure they are unique for (index, _) in zip(items.indices, items) { items[index].id = UUID().uuidString }
// Profiling how fast the operation is, consider elevating this to the UI let timeBeforeAction = Date().timeIntervalSince1970
try await self.$notes.insert(items)
let timeAfterAction = Date().timeIntervalSince1970 print(timeBeforeAction, timeAfterAction, String(format: "%.5fs", timeAfterAction - timeBeforeAction)) } catch { print(error) } }
func removeItems(count: Int) async throws { let removalCount = min(self.notes.count, count)
do { let firstElements = Array(self.notes[0..<removalCount]) try await self.$notes.remove(firstElements) } catch { print(error) } }
func removeAll() async throws { try await self.$notes.removeAll() }}