Contents

bitkey-oss/sharing-firestore

A lightweight wrapper for Firebase's Firestore database that integrates with the Sharing library.

Overview

SharingFirestore is a lightweight wrapper for working with Firebase's Firestore via the Sharing library.

By integrating with Sharing, you can combine Firestore's powerful real-time database capabilities with Sharing's powerful observation functions. You can synchronize data across devices with the ease of UserDefaults, and you can keep your UI updated in real-time, similar to how SwiftUI's @Observable works.

This project is inspired by SharingGRDB and provides a convenient wrapper for using Firestore in SwiftUI and UIKit applications. Thanks to pointfreeco for publishing this great library.

Quick start

Before SharingFirestore's property wrappers can fetch data from Firestore, you need to provide—at runtime—the default Firestore instance it should use. This is typically done as early as possible in your app's lifetime, like the app entry point in SwiftUI:

import SharingFirestore
import SwiftUI

@main
struct MyApp: App {
  init() {
    prepareDependencies {
      FirebaseApp.configure()
      $0.defaultFirestore = Firestore.firestore()
    }
  }
  // ...
}

Note: For more information on preparing Firestore, see [Preparing Firestore][preparing-db-article].

This defaultFirestore connection is used implicitly by SharingFirestore's strategies:

@Shared(
    .sync(
      configuration: .init(
        collectionPath: "todos",
        orderBy: .desc("createdAt"),
        animation: .default
      )
    )
  )
private var todos: IdentifiedArrayOf<Todo>
@SharedReader(
    .query(
      configuration: .init(
        path: "facts",
        predicates: [.order(by: "count", descending: true)],
        animation: .default
      )
    )
  )
private var facts: IdentifiedArrayOf<Fact>

And you can access the Firestore database throughout your application using the dependency system:

@Dependency(\.defaultFirestore)
var database

try database.collection("todos").addDocument(from: Todo(memo: "New todo", completed: false))

This is all you need to know to get started with SharingFirestore, but there's much more to learn. Read the [articles][articles] below to learn how to best utilize this library:

  • [Fetching model data][fetching-article]
  • [Syncing model data][syncing-article]
  • [Observing changes to model data][observing-article]
  • [Preparing Firestore][preparing-db-article]
  • [Dynamic queries][dynamic-queries-article]

[dynamic-queries-article]: https://deepwiki.com/bitkey-oss/sharing-firestore/5.3-dynamic-query-examples [articles]: https://deepwiki.com/bitkey-oss/sharing-firestore/1-sharingfirestore-overview [observing-article]: https://deepwiki.com/bitkey-oss/sharing-firestore/5.2-observable-model-integration [fetching-article]: https://deepwiki.com/bitkey-oss/sharing-firestore/3.1-fetching-data-with-@sharedreader [syncing-article]: https://deepwiki.com/bitkey-oss/sharing-firestore/3.2-syncing-data-with-@shared [preparing-db-article]: https://deepwiki.com/bitkey-oss/sharing-firestore/2.2-setting-up-sharingfirestore

Demos

This repo comes with several examples to demonstrate how to solve common and complex problems with SharingFirestore. Check out this directory to see them all, including:

A number of case studies demonstrating the built-in features of the library, including querying, syncing, dynamic queries, and integration with @Observable models.

Documentation

The documentation for releases and main are available here:

Installation

You can add SharingFirestore to an Xcode project by adding it to your project as a package.

https://github.com/bitkey-oss/sharing-firestore

If you want to use SharingFirestore in a SwiftPM project, it's as simple as adding it to your Package.swift:

dependencies: [
  .package(url: "https://github.com/bitkey-oss/sharing-firestore", from: "0.1.0")
]

And then adding the products to any target that needs access to the libraries:

.product(name: "SharingFirestore", package: "sharing-firestore"),

License

This library is released under the MIT license. See LICENSE for details.

Package Metadata

Repository: bitkey-oss/sharing-firestore

Stars: 28

Forks: 1

Open issues: 0

Default branch: main

Primary language: swift

License: MIT

README: README.md