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
{{ message }}
This repository was archived by the owner on Jan 31, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: doc/offline-mode.md
+29-35Lines changed: 29 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,4 @@
1
1
2
-
3
2
# Offline mode
4
3
5
4
**Table of contents**
@@ -39,20 +38,17 @@ Offline features are brought by Algolia's **Offline SDK**, which is actually com
39
38
40
39
### Prerequisites
41
40
42
-
1. Obtain a **license key** from [Algolia](https://www.algolia.com/).
41
+
- Obtain a **license key** from [Algolia](https://www.algolia.com/).
43
42
44
-
2. Make sure you use an **API key** with the following ACLs:
43
+
- Make sure you use an **API key** with the search (`search`), browse (`browse`) and get index settings (`settings`) ACLs. *This is required because the offline mode needs to replicate the online index's settings and uses browse requests when syncing.*
45
44
46
-
- Search (`search`)
47
-
- Browse (`browse`)
48
-
- Get index settings (`settings`)
49
45
50
-
*This is required because the offline mode needs to replicate the online index's settings and uses browse requests when syncing.*
46
+
### Steps
51
47
52
48
53
49
### Steps
54
50
55
-
2. In your Gradle script, use the `algoliasearch-offline-android` package in place of `algoliasearch-android`. Typically:
51
+
- In your Gradle script, use the `algoliasearch-offline-android` package in place of `algoliasearch-android`. Typically:
56
52
57
53
```groovy
58
54
dependencies {
@@ -61,7 +57,7 @@ Offline features are brought by Algolia's **Offline SDK**, which is actually com
61
57
}
62
58
```
63
59
64
-
3. When initializing your client, instantiate an `OfflineClient` instead of a `Client`. This requires you to specify an additional argument: the directory in which local indices will be stored:
60
+
- When initializing your client, instantiate an `OfflineClient` instead of a `Client`:.
65
61
66
62
```java
67
63
client = new OfflineClient(context, "YOUR_APP_ID", "YOUR_API_KEY");
@@ -70,20 +66,21 @@ Offline features are brought by Algolia's **Offline SDK**, which is actually com
70
66
... where `context` is a valid Android `Context`.
71
67
72
68
By default, the client will store data for local indices in an `algolia` subdirectory of the application's files directory. Alternatively, you may specify it during the instantiation of the client.
73
-
*Warning: although using the cache directory may be tempting, we advise you against doing so. There is no guarantee that all files will be deleted together, and a partial delete could leave an index in an inconsistent state.*
74
69
75
-
4. **Enable offline mode**:
70
+
**Warning:** Although using the cache directory may be tempting, we advise you against doing so. There is no guarantee that all files will be deleted together, and a partial delete could leave an index in an inconsistent state.
An `OfflineClient` provides all the features of an online `Client`. It returns `MirroredIndex` instances, which in turn provide all the features of online `Index` instances.
83
+
An `OfflineClient` provides all the features of an online `Client`. It returns `MirroredIndex` instances, which also provide all the features of online `Index` instances.
87
84
88
85
However, until you explicitly enable the offline mode by calling `enableOfflineMode()`, your offline client behaves like a regular online client. The same goes for indices: *you must explicitly activate mirroring* by calling `setMirrored(true)`. The reason is that you might not want to mirror all of your indices.
89
86
@@ -92,7 +89,7 @@ However, until you explicitly enable the offline mode by calling `enableOfflineM
92
89
93
90
### Synchronization
94
91
95
-
You have entire control over *what* is synchronized and *when*.
92
+
You have entire control over **what** is synchronized and **when**.
96
93
97
94
#### What
98
95
@@ -102,11 +99,11 @@ A *data selection query* is essentially a combination of a browse `Query` and a
102
99
103
100
It will do so for every data selection query you have provided, then build (or re-build) the local index from the retrieved data. (When re-building, the previous version of the local index remains available for querying, until it is replaced by the new version.)
104
101
105
-
*Warning: The entire selected data is re-downloaded at every sync, so be careful about bandwidth usage!*
102
+
**Warning:** The entire selected data is re-downloaded at every sync, so be careful about bandwidth usage!
106
103
107
104
*Warning: It is a programming error to attempt a sync with no data selection queries. Doing so will result in an `IllegalStateException` being thrown.*
108
105
109
-
*Note: Because the sync uses a "browse" to retrieve objects, the number of objects actually mirrored may exceed the maximum object count specified in the data selection query (up to one page of results of difference).*
106
+
**Note:** Because the sync uses a "browse" to retrieve objects, the number of objects actually mirrored may exceed the maximum object count specified in the data selection query (up to one page of results of difference).
110
107
111
108
112
109
#### When
@@ -121,38 +118,36 @@ Alternatively, you may call `MirroredIndex.sync()` to force a sync to happen now
121
118
122
119
The reason you have to choose when to synchronize is that the decision depends on various factors that the SDK cannot know, in particular the specific business rules of your application, or the user's preferences. For example, you may want to sync only when connected to a non-metered Wi-Fi network, or during the night.
123
120
124
-
*Note: Syncs always happen in the background, and therefore should not impact the user's experience.*
121
+
**Note:** Syncs always happen in the background, and therefore should not impact the user's experience.
125
122
126
-
*Note: You cannot have two syncs on the same index running in parallel. If a sync is already running, concurrent sync requests will be ignored.*
123
+
**Note:** You cannot have two syncs on the same index running in parallel. If a sync is already running, concurrent sync requests will be ignored.
127
124
128
125
129
126
### Querying
130
127
131
-
#### Transparent fallback
128
+
#### Transparent fallback mode
132
129
133
-
You query a mirrored index using the same `searchAsync()` method as a purely online index. This will use the offline mirror as a fallback in case of failure of the online request.
130
+
You query a mirrored index using the same `search()` method as a purely online index. This will use the offline mirror as a fallback in case of failure of the online request.
134
131
135
-
There's a catch, however. A mirrored index can function in two modes:
132
+
You can customize this behavior by changing the `requestStrategy` property of the index:
136
133
137
-
1. **Preventive offline search** (default). In this mode, if the online request is too slow to return, an offline request is launched preventively after a certain delay.
134
+
- `FallbackOnFailure` (default) only falls back to the offline mirror if the online request fails. This covers cases where the device is offline; however, when the device is online but with a bad connectivity, every timeout has to be hit before the request is considered a failure.
138
135
139
-
*Warning: This may lead to your completion handler being called twice:* a first time with the offline results, and a second time with the online results. However, if the online request is fast enough (and successful, or the error cannot be recovered), the callback will be called just once.
136
+
- To enforce a maximum response time, you can use `FallbackOnTimeout`, adjusting the timeout threshold via the `offlineFallbackTimeout` property.
140
137
141
-
You may adjust the delay using `MirroredIndex.setPreventiveOfflineSearchDelay()`. The default is `MirroredIndex.DEFAULT_PREVENTIVE_OFFLINE_SEARCH_DELAY`.
138
+
- `OnlineOnly` and `OfflineOnly` are rather straightforward strategies, meant to implement custom strategies on top of them.
142
139
143
-
2. **Offline fallback.** In this mode, the index first tries an online request, and in case of failure, switches back to the offline mirror, but only after the online request has failed. Therefore, the completion handler is guaranteed to be called exactly once.
140
+
#### Data origin
144
141
145
-
You can switch between those two modes by calling `MirroredIndex.setPreventiveOfflineSearch()`.
146
-
147
-
The origin of the data is indicated by the `origin` attribute in the returned result object: its value is `remote` if the content originates from the online API, and `local` if the content originates from the offline mirror.
142
+
The origin of the data is indicated by the `origin` attribute in the returned result object: its value is `remote` if the content originates from the online API, and `local` if the content originates from the offline mirror. When you are using a mixed online/offline request strategy (the default, see above), this lets you know where the response is coming from.
148
143
149
144
150
145
#### Direct query
151
146
152
147
You may directly target the offline mirror if you want:
153
148
154
-
- for search queries: `searchMirrorAsync()`
155
-
- for browse queries: `browseMirrorAsync()` and `browseMirrorFromAsync()`
149
+
- for search queries: `searchOffline()`
150
+
- for browse queries: `browseMirror()` and `browseMirrorFrom()`
156
151
157
152
Those methods have the same semantics as their online counterpart.
158
153
@@ -173,10 +168,10 @@ In order to offer the best user experience, you may want to pre-load some of tho
173
168
To do so:
174
169
175
170
- register a listener on the mirrored index (see [Events](#events) above);
176
-
- when the sync is finished, browse the local mirror, parsing each object to detect associated resources;
171
+
- when the sync is finished, browse the local mirror (using `browseMirror()` and `browseMirrorFrom()`, see above), parsing each object to detect associated resources;
177
172
- pre-fetch those that are not already available locally.
178
173
179
-
*Note: You should do so from a background thread with a low priority to minimize impact on the user's experience.*
174
+
**Note:** You should do so from a background thread with a low priority to minimize impact on the user's experience.
180
175
181
176
182
177
@@ -210,10 +205,9 @@ Any unexpected condition should result in a warning/error being logged.
210
205
211
206
If you think you found a bug in the offline client, please submit an issue on GitHub so that it can benefit the community.
212
207
213
-
If you have a crash in the offline core, please send us a support request. Make sure to include the *crash report*, so that we can pinpoint the problem.
208
+
If you have a crash in the offline core, please send us a support request. Make sure to include the **crash report**, so that we can pinpoint the problem.
214
209
215
210
216
211
## Other resources
217
212
218
-
The offline client bears extensive Javadoc documentation. Please refer to it for more details.
219
-
213
+
The offline client bears extensive documentation comments. Please refer to them for more details.
0 commit comments