I love the fact that you’re using values in the enum cases, then grabbing them in the switch statements. It’s a thing I don’t see a lot of swift devs using.
Thanks for a great tutorial.
For someone just beginning with Swift it has some great techniques.
Attaching [Recordings] to the .populated case is so clean and clever: I love it.
Not to mention reducing the reliance of checking for optionals.
One of the best tutorials / code examples I’ve seen on RW. Bravo, Keegan!
Side Note: Swiftlint has major problems with adding any non-alpha characters in Enum names (such as an Array) - so if you use Swiftlint you’ll have to turn off those rules to get this project to build.
func update(response: RecordingsResult) {
if let error = response.error {
state = .error(error)
return
}
guard let newRecordings = response.recordings,
!newRecordings.isEmpty else {
state = .empty
return
}
var allRecordings = state.currentRecordings
allRecordings.append(contentsOf: newRecordings)
if response.hasMorePages {
state = .paging(allRecordings, next: response.nextPage)
} else {
state = .populated(allRecordings)
}
}
While paging What if Last page returns 0 elements then state = .empty will be executed and after that tableview will show empty view ? Can we fix this ?
The state is only set to .empty if there are no recordings. Only if response.recordings is nil, or if it is an empty array.
If there is a non-empty recordings array, but the RecordingsResult object has multiple pages, we enter the .paging state. Even if a particular page has 0 pages (which should never happen), you will still be in the .paging state, and the view will be populated with the recordings from the other pages.