Skip to content

sergiocampama/swift-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftQuery

Inspired by Tanstack Query, this library allows creating data graphs that use the Observation framework to perform calculations, which could be async or not. These calculations could be anything from API calls, to expensive computations.

This is not a stable project, but an exploration of using the Observation framework in an interesting manner. It also does not offer the same set of features as Tanstack Query, given this is a single day coding experiment.

Benefits

  • Bite-sized async and caching state management. No need for complex data stores that have all available state making compilation and code indexing slow or error prone.
  • Easily integratable for projects, no need to rewrite the entire data stack to start using.

Usage

To declare a data dependency graph, use either SQState for root data, which means, data that doesn't have any dependencies, or SQDerived, which

let pageIndex = SQState(0)
let pageData = SQDerived(key: [pageIndex]) {
  try await Task.sleep(for: .milliseconds(500))
  return PageData(pageIndex: pageIndex.value)
}

Check the Examples directory for demo ViewController that uses this data architecture.

Notes

  • I haven't explored how to make this work better with property wrappers, but given the need to store references to the data, and you don't have access to self when declaring these as properties, I am not sure how feasible it is.
  • Given I wanted to do async computations, I couldn't find a way to avoid the key or deps fields for automatic tracking of used variables inside the async computation method. Maybe in the future withObservationTracking:onChange: will support async somehow.
  • I didn't explore this, but I think it should be easy to extract the trackChange method in the example to make it simple to track changes of certain SQState or SQDerived to only update small portions of the UI and not all of it.
  • Because of some Task.immediate usage, this is for now iOS 26, macOS26, tvOS 26 and watchOS 26 compatible only. Shouldn't be too hard to make it compatible for older versions.
  • Using UserDefaults as an easy store, I'm sure there are better stores out there for this kind of library.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages