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
In `async` fashion, you deal with `Result` instead of `try catch` because the result is delivered at a later time, in order to not block the current calling queue. In the completion block, you either have `value` or `error`.
243
+
In `async` fashion, you deal with `Result` instead of `try catch` because the result is delivered at a later time, in order to not block the current calling queue. In the completion block, you either have `value` or `error`.
241
244
242
245
You access Async APIs via `storage.async`, it is also thread safe, and you can use Sync and Async APIs in any order you want. All Async functions are constrained by `AsyncStorageAware` protocol.
243
246
@@ -290,7 +293,7 @@ storage.async.removeExpiredObjects() { result in
290
293
By default, all saved objects have the same expiry as the expiry you specify in `DiskConfig` or `MemoryConfig`. You can overwrite this for a specific object by specifying `expiry` for `setObject`
291
294
292
295
```swift
293
-
// Default cexpiry date from configuration will be applied to the item
296
+
// Default expiry date from configuration will be applied to the item
294
297
try? storage.setObject("This is a string", forKey: "string")
295
298
296
299
// A given expiry date will be applied to the item
@@ -304,6 +307,61 @@ try? storage.setObject(
304
307
storage.removeExpiredObjects()
305
308
```
306
309
310
+
## Observations
311
+
312
+
[Storage](#storage) allows you to observe changes in the cache layer, both on
313
+
a store and a key levels. The API lets you pass any object as an observer,
314
+
while also passing an observation closure. The observation closure will be
315
+
removed automatically when the weekly captured observer has been deallocated.
316
+
317
+
## Storage observations
318
+
319
+
```swift
320
+
// Add observer
321
+
let token = storage.addStorageObserver(self) { observer, storage, change
322
+
switch change {
323
+
case .add(let key):
324
+
print("Added \(key)")
325
+
case .remove(let key):
326
+
print("Removed \(key)")
327
+
case .removeAll:
328
+
print("Removed all")
329
+
case .removeExpired:
330
+
print("Removed expired")
331
+
}
332
+
}
333
+
334
+
// Remove observer
335
+
token.cancel()
336
+
337
+
// Remove all observers
338
+
storage.removeAllStorageObservers()
339
+
```
340
+
341
+
## Key observations
342
+
343
+
```swift
344
+
let key ="user1"
345
+
346
+
let token = storage.addObserver(self, forKey: key) { observer, storage, change in
347
+
switch change {
348
+
case .edit(let before, let after):
349
+
print("Changed object for \(key) from \(String(describing: before)) to \(after)")
350
+
case .remove:
351
+
print("Removed \(key)")
352
+
}
353
+
}
354
+
355
+
// Remove observer by token
356
+
token.cancel()
357
+
358
+
// Remove observer for key
359
+
storage.removeObserver(forKey: "user1")
360
+
361
+
// Remove all observers
362
+
storage.removeAllKeyObservers()
363
+
```
364
+
307
365
## Handling JSON response
308
366
309
367
Most of the time, our use case is to fetch some json from backend, display it while saving the json to storage for future uses. If you're using libraries like [Alamofire](https://github.com/Alamofire/Alamofire) or [Malibu](https://github.com/hyperoslo/Malibu), you mostly get json in the form of dictionary, string, or data.
@@ -360,7 +418,7 @@ You also need to add `SwiftHash.framework` in your [copy-frameworks](https://git
360
418
## Author
361
419
362
420
-[Hyper](http://hyper.no) made this with ❤️
363
-
- Inline MD5 implementation from [SwiftHash](https://github.com/onmyway133/SwiftHash)
421
+
- Inline MD5 implementation from [SwiftHash](https://github.com/onmyway133/SwiftHash)
0 commit comments