Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pr1mer-tech/gravity
Gravity is a SwiftUI library for remote data fetching. Inspired by Vercel's SWR
https://github.com/pr1mer-tech/gravity
hacktoberfest network swift swiftui
Last synced: about 1 month ago
JSON representation
Gravity is a SwiftUI library for remote data fetching. Inspired by Vercel's SWR
- Host: GitHub
- URL: https://github.com/pr1mer-tech/gravity
- Owner: pr1mer-tech
- Created: 2021-04-04T20:40:30.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-11-25T04:30:41.000Z (about 1 month ago)
- Last Synced: 2024-11-25T05:23:57.956Z (about 1 month ago)
- Topics: hacktoberfest, network, swift, swiftui
- Language: Swift
- Homepage: https://gravity.pr1mer.tech
- Size: 855 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Gravity
## Introduction
Gravity is a SwiftUI library for remote data fetching. It uses **SWR** technonology to fetch data from the server.
The name “**SWR**” is derived from `stale-while-revalidate`, a cache invalidation strategy popularized by [HTTP RFC 5861](https://tools.ietf.org/html/rfc5861).
Gravity first returns the data from cache (stale), then sends the fetch request (revalidate), and finally comes with the up-to-date data again.With Gravity, components will get **a stream of data updates constantly and automatically**. Thus, the UI will be always **fast** and **reactive**.
## Quick Start
#### Installation
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
It is the recommended way to install Gravity.
Once you have your Swift package set up, adding Gravity as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
```swift
dependencies: [
.package(url: "https://github.com/pr1mer-tech/Gravity.git", .upToNextMajor(from: "0.1.0"))
]
```
You can then import and use Gravity:
```swift
import SwiftUI
import Gravitystruct LandmarkList: View {
@SWR(url: "https://example.org/api/endpoint") var api
var body: some View {
if let landmarks = api.data {
List(landmarks, id: \.id) { landmark in
LandmarkRow(landmark: landmark)
}
}
}
}
```In this example, the property wrapper `@SWR` accepts a `url` and a `model`.
The `url` is the URL of the API. And the `model` is a `Codable` object.
`api` will be a `StateResponse` object that has three children: `data` that will contain the decoded data, `error` in case there is an error and `awaiting` when the app is loading.