Skip to content

Commit f13226f

Browse files
Merge pull request #1312 from apollographql/improve/docs
Flesh out docs around cache setup
2 parents 078d38b + 867b18c commit f13226f

4 files changed

Lines changed: 116 additions & 1 deletion

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
ExpansionPanel,
3+
ExpansionPanelList,
4+
ExpansionPanelListItem
5+
} from 'gatsby-theme-apollo-docs';
6+
7+
<ExpansionPanel title="Adding SQLite with Carthage">
8+
9+
You will need to add the `ApolloSQLite` framework to your target. This should be one of the libraries that gets built automatically on checkout, and should include the dependent libraries necessary to run it.
10+
11+
</ExpansionPanel>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
ExpansionPanel,
3+
ExpansionPanelList,
4+
ExpansionPanelListItem
5+
} from 'gatsby-theme-apollo-docs';
6+
7+
<ExpansionPanel title="Adding SQLite with CocoaPods">
8+
9+
Add the following to your `Podfile`:
10+
11+
```ruby
12+
pod 'Apollo'
13+
pod 'Apollo/SQLite'
14+
```
15+
16+
Note that if you're specifying a version for `Apollo`, you need to specify the same version for `Apollo/SQLite`.
17+
18+
</ExpansionPanel>

docs/shared/sqlite-spm-panel.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {
2+
ExpansionPanel,
3+
ExpansionPanelList,
4+
ExpansionPanelListItem
5+
} from 'gatsby-theme-apollo-docs';
6+
7+
<ExpansionPanel title="Adding SQLite with Swift Package Manager">
8+
9+
Add the following to your `Package.swift`:
10+
11+
```swift
12+
.target(
13+
name: "MyApplication",
14+
dependencies: [
15+
.product(name: "ApolloSQLite", package: "Apollo"),
16+
])
17+
```
18+
19+
</ExpansionPanel>
Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: Client-side caching
33
---
44

5+
import SPMSQLite from "../shared/sqlite-spm-panel.mdx"
6+
import CocoaPodsSQLite from "../shared/sqlite-cocoapods-panel.mdx"
7+
import CarthageSQLite from "../shared/sqlite-carthage-panel.mdx"
8+
59
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.
610

711
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.
@@ -15,6 +19,69 @@ All caches used by the `ApolloClient` must conform to the [`NormalizedCache` pro
1519

1620
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.
1721

22+
## Cache Setup
23+
24+
### In-Memory Cache
25+
26+
For `InMemoryNormalizedCache`, no sub-libraries are needed.
27+
28+
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.
29+
30+
If for some reason you find you need to instantiate the in-memory cache yourself, you can do so with one line:
31+
32+
```swift:title=Cache%20Setup
33+
import Apollo
34+
35+
let cache = InMemoryNormalizedCache()
36+
```
37+
38+
### SQLite Cache
39+
40+
To use the `SQLiteNormalizedCache`, you need to add the `ApolloSQLite` sub-library to your project using your preferred package manager:
41+
42+
<SPMSQLite />
43+
44+
<CocoaPodsSQLite />
45+
46+
<CarthageSQLite />
47+
48+
Once added, you can do the following:
49+
50+
1. Set up a file URL for your `SQLite` file.
51+
2. Use that file URL to instantiate a SQLite cache.
52+
3. Use that SQLite cache to instantiate an `ApolloStore`.
53+
4. Pass that `ApolloStore` into the initializer of `ApolloClient`:
54+
55+
```swift:title=Client%20Setup
56+
import Apollo
57+
58+
// NOTE: You need this import line if you are **NOT** using CocoaPods. In CocoaPods,
59+
// ApolloSQLite files are collapsed into the Apollo framework. For other dependency managers,
60+
// ApolloSQLite is a separate framework.
61+
import ApolloSQLite
62+
63+
// 1. You'll have to figure out where to store your SQLite file.
64+
// A reasonable place is the user's Documents directory in your sandbox.
65+
// In any case, create a file URL for your file:
66+
let documentsPath = NSSearchPathForDirectoriesInDomains(
67+
.documentDirectory,
68+
.userDomainMask,
69+
true).first!
70+
let documentsURL = URL(fileURLWithPath: documentsPath)
71+
let sqliteFileURL = documentsURL.appendingPathComponent("test_apollo_db.sqlite")
72+
73+
// 2. Use that file URL to instantiate the SQLite cache:
74+
let sqliteCache = try SQLiteNormalizedCache(fileURL: sqliteFileURL)
75+
76+
// 3. And then instantiate an instance of `ApolloStore` with the cache you've just created:
77+
let store = ApolloStore(cache: sqliteCache)
78+
79+
// 4. Assuming you've set up your `networkTransport` instance elsewhere,
80+
// pass the store you just created into your `ApolloClient` initializer,
81+
// and you're now set up to use the SQLite cache for persistent storage
82+
let apolloClient = ApolloClient(networkTransport: networkTransport, store: store)
83+
```
84+
1885
## Controlling normalization
1986

2087
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.
@@ -107,4 +174,4 @@ store.withinReadWriteTransaction({ transaction in
107174
print(graphQLResult?.data?.hero?.name)
108175
}
109176
})
110-
```
177+
```

0 commit comments

Comments
 (0)