1
- # SwiftDataLoader
2
- SwiftDataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
1
+ # DataLoader
2
+ DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
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
8
9
9
## Installation 💻
10
10
11
11
Update your ` Package.swift ` file.
12
12
13
13
``` swift
14
- .Package (url : " https://github.com/kimdv/SwiftDataLoader .git" , majorVersion : 1 )
14
+ .Package (url : " https://github.com/GraphQLSwift/DataLoader .git" , . upToNextMajor ( from : " 1.1.0 " ) )
15
15
```
16
16
17
17
## Gettings started 🚀
@@ -27,27 +27,27 @@ let userLoader = Dataloader<Int, User>(batchLoadFunction: { keys in
27
27
})
28
28
```
29
29
#### 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 )
30
+ ``` swift
31
+ let future1 = try userLoader.load (key : 1 , on : eventLoopGroup )
32
+ let future2 = try userLoader.load (key : 2 , on : eventLoopGroup )
33
+ let future3 = try userLoader.load (key : 1 , on : eventLoopGroup )
34
34
```
35
35
36
36
Now there is only one thing left and that is to dispathc it ` try userLoader.dispatchQueue(on: req.eventLoop) `
37
37
38
38
The example above will only fetch two users, because the user with key ` 1 ` is present twice in the list.
39
39
40
40
#### 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 )
41
+ There is also a method to load multiple keys at once
42
+ ``` swift
43
+ try userLoader.loadMany (keys : [1 , 2 , 3 ], on : eventLoopGroup )
44
44
```
45
45
46
- ### Disable batching
47
- It is also possible to disable batching ` DataLoaderOptions(batchingEnabled: false) `
48
- It will invoke ` batchLoadFunction ` with a single key
46
+ #### Disable batching
47
+ It is possible to disable batching ` DataLoaderOptions(batchingEnabled: false) `
48
+ It will invoke ` batchLoadFunction ` immediately whenever any key is loaded
49
49
50
- ### Chaching
50
+ ### Caching
51
51
52
52
DataLoader provides a memoization cache for all loads which occur in a single
53
53
request to your application. After ` .load() ` is called once with a given key,
@@ -58,8 +58,8 @@ also creates fewer objects which may relieve memory pressure on your application
58
58
59
59
``` swift
60
60
let userLoader = DataLoader< Int , Int > (... )
61
- let future1 = userLoader.load (1 )
62
- let future2 = userLoader.load (1 )
61
+ let future1 = userLoader.load (key : 1 , on : eventLoopGroup )
62
+ let future2 = userLoader.load (key : 1 , on : eventLoopGroup )
63
63
assert (future1 === future2)
64
64
```
65
65
@@ -91,13 +91,13 @@ Here's a simple example using SQL UPDATE to illustrate.
91
91
let userLoader = DataLoader< Int , Int > (... )
92
92
93
93
// And a value happens to be loaded (and cached).
94
- userLoader.load (4 )
94
+ userLoader.load (key : 4 , on : eventLoopGroup )
95
95
96
96
// A mutation occurs, invalidating what might be in cache.
97
97
sqlRun ('UPDATE users WHERE id= 4 SET username= " zuck" ').then { userLoader.clear (4 ) }
98
98
99
99
// Later the value load is loaded again so the mutated data appears.
100
- userLoader.load (4 )
100
+ userLoader.load (key : 4 , on : eventLoopGroup )
101
101
102
102
// Request completes.
103
103
```
@@ -112,19 +112,19 @@ be cached to avoid frequently loading the same `Error`.
112
112
In some circumstances you may wish to clear the cache for these individual Errors:
113
113
114
114
``` swift
115
- userLoader.load (1 ).catch { error in {
115
+ userLoader.load (key : 1 , on : eventLoopGroup ).catch { error in {
116
116
if (/* determine if should clear error */ ) {
117
- userLoader.clear (1 );
118
- }
119
- throw error
117
+ userLoader.clear (key : 1 );
118
+ }
119
+ throw error
120
120
}
121
121
```
122
122
123
123
#### Disabling Cache
124
124
125
125
In certain uncommon cases, a DataLoader which * does not* cache may be desirable.
126
126
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
127
+ call to `.load ()` will produce a * new* Future, and previously requested keys will not be
128
128
saved in memory.
129
129
130
130
However, when the memoization cache is disabled, your batch function will
@@ -135,13 +135,16 @@ for each instance of the requested key.
135
135
For example:
136
136
137
137
```swift
138
- let myLoader = DataLoader< String , String > (options : DataLoaderOptions (cachingEnabled : false ), batchLoadFunction : { keys in
139
- self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
140
- })
138
+ let myLoader = DataLoader< String , String > (
139
+ options : DataLoaderOptions (cachingEnabled : false ),
140
+ batchLoadFunction : { keys in
141
+ self .someBatchLoader (keys : keys).map { DataLoaderFutureValue.success ($0 ) }
142
+ }
143
+ )
141
144
142
- myLoader.load (" A" )
143
- myLoader.load (" B" )
144
- myLoader.load (" A" )
145
+ myLoader.load (key : " A" , on : eventLoopGroup )
146
+ myLoader.load (key : " B" , on : eventLoopGroup )
147
+ myLoader.load (key : " A" , on : eventLoopGroup )
145
148
146
149
// > [ "A", "B", "A" ]
147
150
```
@@ -171,3 +174,9 @@ When creating a pull request, please adhere to the current coding style where po
171
174
172
175
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
176
[Nicholas Schrock](https :// github.com/schrockn) from [Facebook](https://www.facebook.com/).
177
+
178
+ [swift- badge]: https: // img.shields.io/badge/Swift-5.2-orange.svg?style=flat
179
+ [swift- url]: https: // swift.org
180
+
181
+ [mit- badge]: https: // img.shields.io/badge/License-MIT-blue.svg?style=flat
182
+ [mit- url]: https: // tldrlegal.com/license/mit-license
0 commit comments