This project is a Swift implementation of TanStack Query for SwiftUI applications. This guide helps maintain consistency and quality throughout development.
- principles.md - Core principles, philosophy, and Swift 6 compliance
- api-design.md - API patterns, usage examples
- roadmap.md - Development roadmap with phases and milestones
- feature-parity.md - Complete list of TanStack Query React features to implement
- architecture.md - Technical architecture and component design (planned)
- optimization.md - Performance and memory optimization guidelines (planned)
- networking.md - Network abstraction layer details (planned)
The Documentation folder contains the complete TanStack Query source code. When implementing ANY feature, you MUST reference the original implementation to ensure architectural consistency.
queryClient.ts- Central client managing all queries/mutationsqueryCache.ts- Query state cache implementationmutationCache.ts- Mutation state cache implementationquery.ts- Individual query instance logicmutation.ts- Individual mutation instance logicqueryObserver.ts- Observer pattern for reactive updatesinfiniteQueryObserver.ts- Infinite query implementationretryer.ts- Retry logic with exponential backofffocusManager.ts- Window focus detectiononlineManager.ts- Online/offline detectionnotifyManager.ts- Notification schedulingtypes.ts- Core TypeScript types
useQuery.ts- Query hook implementationuseMutation.ts- Mutation hook implementationuseInfiniteQuery.ts- Infinite query hookuseBaseQuery.ts- Shared query logicQueryClientProvider.tsx- React context pattern
-
ALWAYS check TanStack Query implementation first
- Before implementing any feature, find the corresponding code in Documentation folder
- Study both query-core (logic) and react-query (integration) implementations
- Adapt TypeScript patterns to Swift idioms
-
Follow TanStack Query's Architecture
- Observer pattern for reactive updates
- Separate caches for queries and mutations
- Framework-agnostic core with thin framework wrapper
- Stale-while-revalidate caching strategy
-
Maintain Feature Parity
- Default behaviors (see
Documentation/query/docs/framework/react/guides/important-defaults.md) - Automatic refetching (mount, focus, reconnect)
- Query invalidation and garbage collection
- Structural sharing for performance
- Default behaviors (see
- Swift 6 strict concurrency mode compatible
- Built with Perception library (@Perceptible) for iOS 16+ compatibility
- Minimal external dependencies (only Perception for state observation)
- Match TanStack Query's architecture and behavior
- Referenced corresponding TanStack Query implementation
- All types are Sendable
- Using @Perceptible instead of @Observable for iOS 16+ compatibility
- Matches TanStack Query behavior
- Full DocC documentation
- Unit tests for all public APIs
- Study TanStack Query source in
Documentation/query/packages/ - Review principles.md for Swift-specific adaptations
- Follow api-design.md for SwiftUI integration patterns
- Always reference original TypeScript implementation
- Adapt patterns to Swift 6 concurrency model
When implementing features, run these commands:
swift test
swift build -Xswiftc -strict-concurrency=complete --enable-experimental-prebuiltsIMPORTANT TESTING RULES:
- Use
swift build -Xswiftc -strict-concurrency=complete --enable-experimental-prebuiltsto verify compilation after implementation changes - Use
swift testto verify unit tests after implementation changes. - After each implementation changes, please update/write new unit tests to test existing/new code.
- Never run
xcodebuildcommands on .xcodeproj files for automated testing - The Example/ directory contains Xcode projects for manual testing only
The Example/swiftui-query-demo/ directory contains a complete Pokemon API demo showing real-world SwiftUI Query usage.
To verify the demo project:
# Use Swift Package Manager (recommended for automated testing)
cd /path/to/swiftui-query
swift test # Verifies all core functionality works
swift build -Xswiftc -strict-concurrency=complete --enable-experimental-prebuilts
# For manual testing in Xcode simulator (optional)
# Open Example/swiftui-query-demo.xcodeproj in Xcode and run manually
# Note: xcodebuild commands may have simulator/device dependenciesImportant: The main library verification should always use swift test and swift build commands. The Example Xcode project is for manual UI testing and demonstrating real-world usage patterns.
From the official documentation:
- Query results are cached and considered stale immediately
- Stale queries refetch automatically on:
- New instances mount
- Window refocus
- Network reconnect
- Optional: at configured intervals
- Inactive queries garbage collected after 5 minutes
- Failed queries retry 3 times with exponential backoff
- Query results are structurally shared to detect changes
- Read documents in @Documentation/query/docs/framework/react/
- When implementing swift version of react query, always looks for latest implementation details from react query. Behavior of enum, public API should be kept closest as much as possible
- Remember to add/update unit tests after each task
- After each task, run make format and make lint / make lint-fix and fix issues if needed. Then commit code.
- Components should be placed in each separated file. Don't write everything into one big file!
- After each tasks, update api-design, roadmap checklist and feature parity if needs.