A Swift package for efficient data fetching and caching with built-in support for pagination and SQLite persistence.
CachedData is a lightweight, type-safe framework designed to simplify data fetching, caching, and retrieval in iOS and macOS applications. It provides a robust solution for managing remote data with local persistence, handling pagination, and managing data views.
- Type-safe data fetching: Generic adapters for different data sources
- Automatic caching: Persistent storage of fetched data in SQLite database
- Pagination support: Built-in handling for cursor-based pagination
- Data views: Group and access related cached items efficiently
- Error handling: Comprehensive error types and user-friendly messages
- SwiftUI integration: Observable objects for seamless UI updates
- iOS 17.0+ / macOS 14.0+
- Swift 6.1+
- Xcode 15.0+
Add CachedData to your project by adding it as a dependency in your Package.swift file:
dependencies: [
.package(url: "https://github.com/yourusername/cached-data.git", from: "1.0.0")
]First, ensure the cache database is properly initialized:
// Access the shared database
let database = DatabaseQueue.observableModelDatabase- Define your data model conforming to
DataFetcherItem:
struct MyItem: DataFetcherItem {
let id: UUID
let name: String
var stringId: String { id.uuidString }
func toCacheItem() -> StoredCacheItem {
let encoder = JSONEncoder()
let data = try! encoder.encode(self)
return StoredCacheItem(
id: stringId,
type_name: String(describing: Self.self),
created_at: Date(),
json_string: String(data: data, encoding: .utf8)!
)
}
init(fromCache item: StoredCacheItem) {
let decoder = JSONDecoder()
let data = item.json_string.data(using: .utf8)!
self = try! decoder.decode(Self.self, from: data)
}
}- Create your fetcher adapter:
struct MyAdapter: DataFetcherAdapter {
typealias Item = MyItem
var params: MyParams
var pageInfo: MyPageInfo?
init(params: MyParams) {
self.params = params
}
func fetch() async throws -> ([MyItem], MyPageInfo) {
// Implement your network request logic here
}
}- Instantiate and use the fetcher:
let fetcher = DataFetcher(adapter: MyAdapter(params: MyParams()))
// Load data
await fetcher.load()
// Access items
let items = fetcher.items- SharingGRDB - SQLite database management
- ErrorKit - Error handling utilities
This project is available under the MIT license.