@@ -3,15 +3,17 @@ SwiftDataLoader is a generic utility to be used as part of your application's da
3
3
4
4
This is a Swift version of the Facebook [ DataLoader] ( https://github.com/facebook/dataloader ) .
5
5
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 ]
8
10
9
11
## Installation 💻
10
12
11
13
Update your ` Package.swift ` file.
12
14
13
15
``` 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 " ) )
15
17
```
16
18
17
19
## Gettings started 🚀
@@ -27,27 +29,27 @@ let userLoader = Dataloader<Int, User>(batchLoadFunction: { keys in
27
29
})
28
30
```
29
31
#### 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 )
34
36
```
35
37
36
38
Now there is only one thing left and that is to dispathc it ` try userLoader.dispatchQueue(on: req.eventLoop) `
37
39
38
40
The example above will only fetch two users, because the user with key ` 1 ` is present twice in the list.
39
41
40
42
#### 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 )
44
46
```
45
47
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
49
51
50
- ### Chaching
52
+ ### Caching
51
53
52
54
DataLoader provides a memoization cache for all loads which occur in a single
53
55
request 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
58
60
59
61
``` swift
60
62
let 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 )
63
65
assert (future1 === future2)
64
66
```
65
67
@@ -91,13 +93,13 @@ Here's a simple example using SQL UPDATE to illustrate.
91
93
let userLoader = DataLoader< Int , Int > (... )
92
94
93
95
// And a value happens to be loaded (and cached).
94
- userLoader.load (4 )
96
+ userLoader.load (key : 4 , on : eventLoopGroup )
95
97
96
98
// A mutation occurs, invalidating what might be in cache.
97
99
sqlRun ('UPDATE users WHERE id= 4 SET username= " zuck" ').then { userLoader.clear (4 ) }
98
100
99
101
// Later the value load is loaded again so the mutated data appears.
100
- userLoader.load (4 )
102
+ userLoader.load (key : 4 , on : eventLoopGroup )
101
103
102
104
// Request completes.
103
105
```
@@ -112,19 +114,19 @@ be cached to avoid frequently loading the same `Error`.
112
114
In some circumstances you may wish to clear the cache for these individual Errors:
113
115
114
116
``` swift
115
- userLoader.load (1 ).catch { error in {
117
+ userLoader.load (key : 1 , on : eventLoopGroup ).catch { error in {
116
118
if (/* determine if should clear error */ ) {
117
- userLoader.clear (1 );
118
- }
119
- throw error
119
+ userLoader.clear (key : 1 );
120
+ }
121
+ throw error
120
122
}
121
123
```
122
124
123
125
#### Disabling Cache
124
126
125
127
In certain uncommon cases, a DataLoader which * does not* cache may be desirable.
126
128
Calling `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
128
130
saved in memory.
129
131
130
132
However, when the memoization cache is disabled, your batch function will
@@ -135,13 +137,16 @@ for each instance of the requested key.
135
137
For example:
136
138
137
139
```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
+ )
141
146
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 )
145
150
146
151
// > [ "A", "B", "A" ]
147
152
```
@@ -171,3 +176,15 @@ When creating a pull request, please adhere to the current coding style where po
171
176
172
177
This library is entirely a Swift version of Facebooks [DataLoader](https :// github.com/facebook/dataloader). Developed by [Lee Byron](https://github.com/leebyron) and
173
178
[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