@@ -3,15 +3,17 @@ SwiftDataLoader is a generic utility to be used as part of your application's da
33
44This is a Swift version of the Facebook [ DataLoader] ( https://github.com/facebook/dataloader ) .
55
6- [ ![ CircleCI] ( https://circleci.com/gh/kimdv/SwiftDataLoader.svg?style=svg )] ( https://circleci.com/gh/kimdv/SwiftDataLoader )
7- [ ![ codecov] ( https://codecov.io/gh/kimdv/SwiftDataLoader/branch/master/graph/badge.svg )] ( https://codecov.io/gh/kimdv/SwiftDataLoader )
6+ [ ![ Swift] [ swift-badge ]] [ swift-url ]
7+ [ ![ License] [ mit-badge ]] [ mit-url ]
8+ [ ![ CircleCI] [ circleci-badge ]] [ circleci-uri ]
9+ [ ![ codecov] [ codecov-badge ]] [ codecov-uri ]
810
911## Installation 💻
1012
1113Update your ` Package.swift ` file.
1214
1315``` swift
14- .Package (url : " https://github.com/kimdv /SwiftDataLoader.git" , majorVersion : 1 )
16+ .Package (url : " https://github.com/GraphQLSwift /SwiftDataLoader.git" , . upToNextMajor ( from : " 1.1.0 " ) )
1517```
1618
1719## Gettings started 🚀
@@ -27,27 +29,27 @@ let userLoader = Dataloader<Int, User>(batchLoadFunction: { keys in
2729})
2830```
2931#### Load single key
30- ```
31- let future1 = try userLoader.load(key: 1, on: req )
32- let future2 = try userLoader.load(key: 2, on: req )
33- let future3 = try userLoader.load(key: 1, on: req )
32+ ``` swift
33+ let future1 = try userLoader.load (key : 1 , on : eventLoopGroup )
34+ let future2 = try userLoader.load (key : 2 , on : eventLoopGroup )
35+ let future3 = try userLoader.load (key : 1 , on : eventLoopGroup )
3436```
3537
3638Now there is only one thing left and that is to dispathc it ` try userLoader.dispatchQueue(on: req.eventLoop) `
3739
3840The example above will only fetch two users, because the user with key ` 1 ` is present twice in the list.
3941
4042#### Load multiple keys
41- There is also an API to load multiple keys at once
42- ```
43- try userLoader.loadMany(keys: [1, 2, 3], on: req.eventLoop )
43+ There is also a method to load multiple keys at once
44+ ``` swift
45+ try userLoader.loadMany (keys : [1 , 2 , 3 ], on : eventLoopGroup )
4446```
4547
46- ### Disable batching
47- It is also possible to disable batching ` DataLoaderOptions(batchingEnabled: false) `
48- It will invoke ` batchLoadFunction ` with a single key
48+ #### Disable batching
49+ It is possible to disable batching ` DataLoaderOptions(batchingEnabled: false) `
50+ It will invoke ` batchLoadFunction ` immediately whenever any key is loaded
4951
50- ### Chaching
52+ ### Caching
5153
5254DataLoader provides a memoization cache for all loads which occur in a single
5355request to your application. After ` .load() ` is called once with a given key,
@@ -58,8 +60,8 @@ also creates fewer objects which may relieve memory pressure on your application
5860
5961``` swift
6062let userLoader = DataLoader< Int , Int > (... )
61- let future1 = userLoader.load (1 )
62- let future2 = userLoader.load (1 )
63+ let future1 = userLoader.load (key : 1 , on : eventLoopGroup )
64+ let future2 = userLoader.load (key : 1 , on : eventLoopGroup )
6365assert (future1 === future2)
6466```
6567
@@ -91,13 +93,13 @@ Here's a simple example using SQL UPDATE to illustrate.
9193let userLoader = DataLoader< Int , Int > (... )
9294
9395// And a value happens to be loaded (and cached).
94- userLoader.load (4 )
96+ userLoader.load (key : 4 , on : eventLoopGroup )
9597
9698// A mutation occurs, invalidating what might be in cache.
9799sqlRun ('UPDATE users WHERE id= 4 SET username= " zuck" ').then { userLoader.clear (4 ) }
98100
99101// Later the value load is loaded again so the mutated data appears.
100- userLoader.load (4 )
102+ userLoader.load (key : 4 , on : eventLoopGroup )
101103
102104// Request completes.
103105```
@@ -112,19 +114,19 @@ be cached to avoid frequently loading the same `Error`.
112114In some circumstances you may wish to clear the cache for these individual Errors:
113115
114116``` swift
115- userLoader.load (1 ).catch { error in {
117+ userLoader.load (key : 1 , on : eventLoopGroup ).catch { error in {
116118 if (/* determine if should clear error */ ) {
117- userLoader.clear (1 );
118- }
119- throw error
119+ userLoader.clear (key : 1 );
120+ }
121+ throw error
120122}
121123```
122124
123125#### Disabling Cache
124126
125127In certain uncommon cases, a DataLoader which * does not* cache may be desirable.
126128Calling `DataLoader (options : DataLoaderOptions (cachingEnabled : false ), batchLoadFunction : batchLoadFunction)` will ensure that every
127- call to `.load ()` will produce a * new* Future, and requested keys will not be
129+ call to `.load ()` will produce a * new* Future, and previously requested keys will not be
128130saved in memory.
129131
130132However, when the memoization cache is disabled, your batch function will
@@ -135,13 +137,16 @@ for each instance of the requested key.
135137For example:
136138
137139```swift
138- let myLoader = DataLoader< String , String > (options : DataLoaderOptions (cachingEnabled : false ), batchLoadFunction : { keys in
139- self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
140- })
140+ let myLoader = DataLoader< String , String > (
141+ options : DataLoaderOptions (cachingEnabled : false ),
142+ batchLoadFunction : { keys in
143+ self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
144+ }
145+ )
141146
142- myLoader.load (" A" )
143- myLoader.load (" B" )
144- myLoader.load (" A" )
147+ myLoader.load (key : " A" , on : eventLoopGroup )
148+ myLoader.load (key : " B" , on : eventLoopGroup )
149+ myLoader.load (key : " A" , on : eventLoopGroup )
145150
146151// > [ "A", "B", "A" ]
147152```
@@ -171,3 +176,15 @@ When creating a pull request, please adhere to the current coding style where po
171176
172177This library is entirely a Swift version of Facebooks [DataLoader](https :// github.com/facebook/dataloader). Developed by [Lee Byron](https://github.com/leebyron) and
173178[Nicholas Schrock](https :// github.com/schrockn) from [Facebook](https://www.facebook.com/).
179+
180+ [swift- badge]: https: // img.shields.io/badge/Swift-5.2-orange.svg?style=flat
181+ [swift- url]: https: // swift.org
182+
183+ [mit- badge]: https: // img.shields.io/badge/License-MIT-blue.svg?style=flat
184+ [mit- url]: https: // tldrlegal.com/license/mit-license
185+
186+ [circleci- badge]: https: // circleci.com/gh/kimdv/SwiftDataLoader.svg?style=svg
187+ [circleci- uri]: https: // circleci.com/gh/kimdv/SwiftDataLoader
188+
189+ [codecov- badge]: https: // codecov.io/gh/kimdv/SwiftDataLoader/branch/master/graph/badge.svg
190+ [codecov- uri]: https: // codecov.io/gh/kimdv/SwiftDataLoader
0 commit comments