Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/shared/sqlite-carthage-panel.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
ExpansionPanel,
ExpansionPanelList,
ExpansionPanelListItem
} from 'gatsby-theme-apollo-docs';

<ExpansionPanel title="Adding SQLite with Carthage">

You will need to add the `ApolloSQLite` framework to your target. This should be one of the libraries which gets built automatically on checkout, and should include the dependent libraries necessary to run it.

</ExpansionPanel>
18 changes: 18 additions & 0 deletions docs/shared/sqlite-cocoapods-panel.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
ExpansionPanel,
ExpansionPanelList,
ExpansionPanelListItem
} from 'gatsby-theme-apollo-docs';

<ExpansionPanel title="Adding SQLite with CocoaPods">

Add the following to your `Podfile`:

```ruby
pod 'Apollo'
pod 'Apollo/SQLite'
```

Note that if you're specifying a version for `Apollo`, you need to specify the same version for `Apollo/SQLite`.

</ExpansionPanel>
19 changes: 19 additions & 0 deletions docs/shared/sqlite-spm-panel.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
ExpansionPanel,
ExpansionPanelList,
ExpansionPanelListItem
} from 'gatsby-theme-apollo-docs';

<ExpansionPanel title="Adding SQLite with Swift Package Manager">

Add the following to your `Package.swift`:

```swift
.target(
name: "MyApplication",
dependencies: [
.product(name: "ApolloSQLite", package: "Apollo"),
])
```

</ExpansionPanel>
61 changes: 61 additions & 0 deletions docs/source/caching.md → docs/source/caching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
title: Client-side caching
---

import SPMSQLite from "../shared/sqlite-spm-panel.mdx"
import CocoaPodsSQLite from "../shared/sqlite-cocoapods-panel.mdx"
import CarthageSQLite from "../shared/sqlite-carthage-panel.mdx"

As mentioned in the introduction, Apollo iOS does more than simply run your queries against a GraphQL server. It normalizes query results to construct a client-side cache of your data, which is kept up to date as further queries and mutations are run.

This means your UI is always internally consistent, and can be kept fully up-to-date with the state on the server with the minimum number of queries required.
Expand All @@ -15,6 +19,63 @@ All caches used by the `ApolloClient` must conform to the [`NormalizedCache` pro

All caches can be cleared in their entirety by calling [`clear(callbackQueue:completion:)`](api/Apollo/protocols/NormalizedCache/#clearcallbackqueuecompletion). If you need to work more directly with the cache, please see the [Direct Cache Access](#direct-cache-access) section.

## Cache Setup

### In-Memory Cache

For `InMemoryNormalizedCache`, no sub-libraries are needed.

This type of cache is used by default when setting up an `ApolloClient`. If you want to use an in-memory cache without modifications, all you have to do is instantiate an `ApolloClient` instance and not pass anything into the `store` parameter.

If for some reason you find you need to instantiate the in-memory cache yourself, you can do so with one line:

```swift:title=Cache%20Setup
import Apollo

let cache = InMemoryNormalizedCache()
```

### SQLite Cache
To use the `SQLiteNormalizedCache`, you must bring in the `ApolloSQLite` sub-library using your package manager of choice:

<SPMSQLite />

<CocoaPodsSQLite />

<CarthageSQLite />

You can then set up a file URL for your `SQLite` file, use that file URL to instantiate a SQLite cache, and then use that SQLite cache to instantiate an `ApolloStore`, which you then pass into `ApolloClient`'s initializer:

```swift:title=Client%20Setup
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nice, does this %20 thing work? Need to keep this in mind 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, found that hack a couple weeks ago:
Screen Shot 2020-07-14 at 6 43 12 PM

import Apollo

// NOTE: You need this import line if you are **NOT** using CocoaPods. In CocoaPods,
// ApolloSQLite files are collapsed into the Apollo framework. For other dependency managers,
// ApolloSQLite is a separate framework.
import ApolloSQLite

// You'll have to figure out where to store your SQLite file.
// A reasonable place is the user's Documents directory in your sandbox.
// In any case, create a file URL for your file:
let documentsPath = NSSearchPathForDirectoriesInDomains(
.documentDirectory,
.userDomainMask,
true).first!
let documentsURL = URL(fileURLWithPath: documentsPath)
let sqliteFileURL = documentsURL.appendingPathComponent("test_apollo_db.sqlite")

// Use that file URL to instantiate the SQLite cache:
let sqliteCache = try SQLiteNormalizedCache(fileURL: sqliteFileURL)

// And then instantiate an instance of `ApolloStore` with the cache you've just created:
let store = ApolloStore(cache: sqliteCache)

// Assuming you've set up your `networkTransport` instance elsewhere,
// pass the store you just created into your `ApolloClient` initializer,
// and you're now set up to use the SQLite cache for persistent storage
let apolloClient = ApolloClient(networkTransport: networkTransport, store: store)
```

## Controlling normalization

While Apollo can do basic caching based on the shape of GraphQL queries and their results, Apollo won't be able to associate objects fetched by different queries without additional information about the identities of the objects returned from the server.
Expand Down