You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<ExpansionPaneltitle="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.
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.
6
10
7
11
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
15
19
16
20
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.
17
21
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
+
18
85
## Controlling normalization
19
86
20
87
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
0 commit comments