Skip to content

Commit

Permalink
review: NextBatchContext lock 코드 제거 및 주석 보강
Browse files Browse the repository at this point in the history
  • Loading branch information
ppth0608 committed Jun 3, 2024
1 parent db81e4f commit defa251
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,17 @@ Here's how to set up pagination:
)
```

4. Once you’ve finished fetching your data, it is very important to let KarrotListKit know that you have finished the process. To do that, you need to call `completeBatchFetching()` method of `NextBatchContext`. This assures that the whole batch fetching mechanism stays in sync and the next batch fetching cycle can happen.
4. Once you’ve finished fetching your data, it is very important to let KarrotListKit know that you have finished the process. To do that, you need to call `completeBatchFetching()` method of `NextBatchContext` on Main-Thread(for avoid data race.). This assures that the whole batch fetching mechanism stays in sync and the next batch fetching cycle can happen.
```swift
List(sections: [])
.onNextBatchTrigger(
decisionProvider: decisionProvider,
handler: { event in
let nextBatchContext = event.context
fetchNextPage() {
nextBatchContext.completeBatchFetching()
DispatchQueue.main.async {
nextBatchContext.completeBatchFetching()
}
}
}
)
Expand Down
4 changes: 3 additions & 1 deletion Sources/KarrotListKit/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ extension List {
/// handler: { [weak self] context in
/// self?.context = context
/// self?.fetchNextPage {
/// self?.context.completeBatchFetching()
/// DispatchQueue.main.async {
/// self?.context.completeBatchFetching()
/// }
/// }
/// }
///)
Expand Down
7 changes: 2 additions & 5 deletions Sources/KarrotListKit/NextBatchTrigger/NextBatchContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ public class NextBatchContext {
/// The current state of the batch fetching process.
private(set) var state: State = .completed

/// A lock to ensure thread safety when updating the state.
private let lock = NSLock()

/// Begins the batch fetching process by setting the state to `fetching`.
func beginBatchFetching() {
lock.lock(); defer { self.lock.unlock() }
state = .fetching
}

/// Completes the batch fetching process by setting the state to `completed`.
///
/// - Important: Call this function on **Main Thread** for avoid data race.
public func completeBatchFetching() {
lock.lock(); defer { self.lock.unlock() }
state = .completed
}
}

0 comments on commit defa251

Please sign in to comment.