My code is not printing in the right order. I think its a problem with the way I'm using dispatch to main. I want to pass in a boss ID. Find the User with that ID then get their SubscribedBosses Array. For every ID in that array query that user and return that users screenName. Append those screenName to the userArray. After that Complete the GetBossSubs function and return userArray (array of screenNames).
Right now result of the .onAppear is running before the function in .onAppear is actually completed. User Appended prints before Found TestUserName
static func getBossSubs(bossID: String, completion: #escaping (Result<UserNames, Error>) ->
()) {
let pred = NSPredicate(format: "uniqueID = %#", bossID)
let sort = NSSortDescriptor(key: "creationDate", ascending: false)
let query = CKQuery(recordType: RecordType.Users, predicate: pred)
query.sortDescriptors = [sort]
let operation = CKQueryOperation(query: query)
operation.desiredKeys = ["subscribedBosses"]
operation.resultsLimit = 50
operation.recordFetchedBlock = { record in
DispatchQueue.main.async {
guard let subs = record["subscribedBosses"] as? [String] else {
print("Error at screenName")
completion(.failure(CloudKitHelperError.castFailure))
return
}
let userArray = UserNames() //Error that it should be a LET is here.
for boss in subs{
CloudKitHelper.getBossScreenName(bossID: boss) { (result) in
switch result{
case .success(let name):
userArray.names.append(name) //works fine
print("Found \(userArray.names)") //Prints a name
case .failure(let er):
completion(.failure(er))
}
}
}
print("does this run?") // Only runs if No Name
completion(.success(userArray)) // contains no name or doesn't run?
}
}
operation.queryCompletionBlock = { (_, err) in
DispatchQueue.main.async {
if let err = err {
completion(.failure(err))
return
}
}
}
CKContainer.default().publicCloudDatabase.add(operation)
}
I call the code like this:
.onAppear {
// MARK: - fetch from CloudKit
self.userList.names = []
let myUserID = UserDefaults.standard.string(forKey: self.signInWithAppleManager.userIdentifierKey)!
// get my subs projects
CloudKitHelper.getBossSubs(bossID: myUserID) { (results) in
switch results{
case .success(let user):
print("Users appended")
self.userList.names = user.names
case .failure(let er):
print(er.localizedDescription)
}
}
}
This is because you run an asynchronous function inside another asynchronous function.
// this just *starts* a series of asynchronous functions, `result` is not yet available
for boss in subs {
CloudKitHelper.getBossScreenName(bossID: boss) { result in
switch result {
case .success(let name):
userArray.names.append(name) // works fine
print("Found \(userArray.names)") // Prints a name
case .failure(let er):
completion(.failure(er))
}
}
}
// this will run before any `CloudKitHelper.getBossScreenName` finishes
completion(.success(userArray))
A possible solution may be to check if all CloudKitHelper.getBossScreenName functions finish (eg. by checking the size of the userArray) and only then return the completion:
for boss in subs {
CloudKitHelper.getBossScreenName(bossID: boss) { result in
switch result {
case .success(let name):
userArray.names.append(name)
if userArray.names.count == subs.count {
completion(.success(userArray)) // complete only when all functions finish
}
case .failure(let er):
completion(.failure(er))
}
}
}
// do not call completion here, wait for all functions to finish
// completion(.success(userArray))
Related
func getStudents() {
var student: Student = Student()
db.collection(StudentViewModel.studentCollection).addSnapshotListener { (querySnapshot, error) in
guard error == nil else {
print("ERROR | Getting Student Documents in Firestore Service: \(String(describing: error))")
return
}
guard let snapshot = querySnapshot else {
// no documents
print("No documents to fetch!")
return
}
DispatchQueue.main.sync {
var updatedStudentDocuments: [Student] = [Student]()
for studentDocument in snapshot.documents {
student = Student(id: studentDocument.documentID,
name: studentDocument.data()["name"] as? String ?? "")
updatedStudentDocuments.append(student)
}
self.students = updatedStudentDocuments
}
}
}
Every time I run this function and check what's inside self.students, I find that it's empty. That's because the function getStudents is returning before the closure in addSnapshotListener finishes executing. How do I make the getStudents function wait for the closure to finish executing before continuing its own execution?
I have two Publishers, I want to feed the second one with the result of first one, I could do what I wanted by calling the second one nested into the first one, it works but it does not feel good to look at, is there a better way to do it?
the first Publisher returns AnyPublisher<URL, Error> and the second one returns AnyPublisher<JsonModel, Error>
func upload(input: URL, output: URL) {
let converter = MP3Converter()
converter.convert(input: input, output: output)
.sink { [weak self] result in
if case .failure(let error) = result {
ConsoleLogger.log(error)
self?.uploadedRecordingURL = nil
}
} receiveValue: { url in
guard let data = try? Data(contentsOf: url) else {
return
}
self.repository.uploadCover(data: data)
.sink(receiveCompletion: { [weak self] result in
if case .failure(let error) = result {
ConsoleLogger.log(error)
self?.uploadedRecordingURL = nil
}
}, receiveValue: { [weak self] response in
self?.uploadedRecordingURL = response.fileURL
}).store(in: &self.disposables)
}.store(in: &disposables)
}
I think you've already deduced this, but just do be clear: you are using sink and its receiveValue completely wrong. Don't start a new chain or do any significant work here at all! There should be a simple sink at the end, followed by store to anchor the chain, and that's the end.
You are looking for flatMap. That is how you chain publishers. (See my https://www.apeth.com/UnderstandingCombine/operators/operatorsTransformersBlockers/operatorsflatmap.html.) You may have to give some thought to exactly what needs to pass from the first publisher and its chain down into the flatMap closure and what needs to pass on down the chain from there.
You could use flatmap to chain your publishers together.
// the code would roughly look like this
converter.convert(input: input, output: output).flatMap { data in
return self.repository.uploadCover(data: data)
}.sink(receiveCompletion: { [weak self] result in
if case .failure(let error) = result {
ConsoleLogger.log(error)
self?.uploadedRecordingURL = nil
}
}, receiveValue: { [weak self] response in
self?.uploadedRecordingURL = response.fileURL
}).store(in: &self.disposables)
Here's an example in playgrounds that compiles
import Combine
import Foundation
enum SomeError: Error {
}
let subject0 = CurrentValueSubject<Data, SomeError>(Data())
let pub0 = subject0.eraseToAnyPublisher()
func repoUpload(data: Data) -> AnyPublisher<URL, SomeError> {
// do the real work, this is just to get it to compile
let subject1 = CurrentValueSubject<URL, SomeError>(URL(fileURLWithPath: "Somepath"))
return subject1.eraseToAnyPublisher()
}
var disposables = Set<AnyCancellable>()
pub0.flatMap { data in
return repoUpload(data: data)
}.sink(receiveCompletion: { result in
print(result)
}, receiveValue: { response in
print(response)
}).store(in: &disposables)
Might I be so inclined to ask for a hand and or different perspectives on how to Unit Test a function on my Viewcontroller that calls an HTTP request to a Back End server using promise kit which returns JSON that is then decoded into the data types needed and then mapped.
This is one of the promise kit functions (called in viewWillAppear) to get stock values etc...
func getVantage(stockId: String) {
firstly {
self.view.showLoading()
}.then { _ in
APIService.Chart.getVantage(stockId: stockId)
}.compactMap {
return $0.dataModel()
}.done { [weak self] data in
guard let self = self else { return }
self.stockValue = Float(data.price ?? "") ?? 0.00
self.valueIncrease = Float(data.delta ?? "") ?? 0.00
self.percentageIncrease = Float(data.deltaPercentage ?? "") ?? 0.00
let roundedPercentageIncrease = String(format: "%.2f", self.percentageIncrease)
self.stockValueLabel.text = "\(self.stockValue)"
self.stockValueIncreaseLabel.text = "+\(self.valueIncrease)"
self.valueIncreasePercentLabel.text = "(+\(roundedPercentageIncrease)%)"
}.ensure {
self.view.hideLoading()
}.catch { [weak self] error in
guard let self = self else { return }
self.handleError(error: error)
}
}
I've thought of using expectations to wait until the promise kit function is called in the unit test like so :
func testChartsMain_When_ShouldReturnTrue() {
//Arange
let sut = ChartsMainViewController()
let exp = expectation(description: "")
let testValue = sut.stockValue
//Act
-> Note : this code down here doesn't work
-> normally a completion block then kicks in and asserts a value then checks if it fulfills the expectation, i'm not mistaken xD
-> But this doesn't work using promisekit
//Assert
sut.getVantage(stockId: "kj3i19") {
XCTAssert((testValue as Any) is Float && !(testValue == 0.0))
exp.fulfill()
}
self.wait(for: [exp], timeout: 5)
}
but the problem is promisekit is done in its own custom chain blocks with .done being the block that returns a value from the request, thus i can't form the completion block on the unit test like in conventional Http requests like :
sut.executeAsynchronousOperation(completion: { (error, data) in
XCTAssertTrue(error == nil)
XCTAssertTrue(data != nil)
testExpectation.fulfill()
})
You seem to have an awful amount of business logic in your view controller, and this is something that makes it harder (not impossible, but harder) to properly test your code.
Recommending to extract all networking and data processing code into the (View)Model of that controller, and expose it via a simple interface. This way your controller becomes as dummy as possible, and doesn't need much unit testing, and you'll be focusing the unit tests on the (view)model.
But that's another, long, story, and I deviate from the topic of this question.
The first thing that prevents you from properly unit testing your function is the APIService.Chart.getVantage(stockId: stockId), since you don't have control over the behaviour of that call. So the first thing that you need to do is to inject that api service, either in the form of a protocol, or in the form of a closure.
Here's the closure approach exemplified:
class MyController {
let getVantageService: (String) -> Promise<MyData>
func getVantage(stockId: String) {
firstly {
self.view.showLoading()
}.then { _ in
getVantageService(stockId)
}.compactMap {
return $0.dataModel()
}.done { [weak self] data in
// same processing code, removed here for clarity
}.ensure {
self.view.hideLoading()
}.catch { [weak self] error in
guard let self = self else { return }
self.handleError(error: error)
}
}
}
Secondly, since the async call is not exposed outside of the function, it's harder to set a test expectation so the unit tests can assert the data once it knows. The only indicator of this function's async calls still running is the fact that the view shows the loading state, so you might be able to make use of that:
let loadingPredicate = NSPredicate(block: { _, _ controller.view.isLoading })
let vantageExpectation = XCTNSPredicateExpectation(predicate: loadingPredicate, object: nil)
With the above setup in place, you can use expectations to assert the behaviour you expect from getVantage:
func test_getVantage() {
let controller = MyController(getVantageService: { _ in .value(mockedValue) })
let loadingPredicate = NSPredicate(block: { _, _ !controller.view.isLoading })
let loadingExpectation = XCTNSPredicateExpectation(predicate: loadingPredicate, object: nil)
controller.getVantage(stockId: "abc")
wait(for: [loadingExpectation], timeout: 1.0)
// assert the data you want to check
}
It's messy, and it's fragile, compare this to extracting the data and networking code to a (view)model:
struct VantageDetails {
let stockValue: Float
let valueIncrease: Float
let percentageIncrease: Float
let roundedPercentageIncrease: String
}
class MyModel {
let getVantageService: (String) -> Promise<VantageDetails>
func getVantage(stockId: String) {
firstly {
getVantageService(stockId)
}.compactMap {
return $0.dataModel()
}.map { [weak self] data in
guard let self = self else { return }
return VantageDetails(
stockValue: Float(data.price ?? "") ?? 0.00,
valueIncrease: Float(data.delta ?? "") ?? 0.00,
percentageIncrease: Float(data.deltaPercentage ?? "") ?? 0.00,
roundedPercentageIncrease: String(format: "%.2f", self.percentageIncrease))
}
}
}
func test_getVantage() {
let model = MyModel(getVantageService: { _ in .value(mockedValue) })
let vantageExpectation = expectation(name: "getVantage")
model.getVantage(stockId: "abc").done { vantageData in
// assert on the data
// fulfill the expectation
vantageExpectation.fulfill()
}
wait(for: [loadingExpectation], timeout: 1.0)
}
I have a class called QueryObserver that can produce multiple results over time, given back as callbacks (closures). You use it like this:
let observer = QueryObserver<ModelType>(query: query) { result in
switch result {
case .success(let value):
print("result: \(value)")
case .failure(let error):
print("error: \(error)")
}
}
(QueryObserver is actually a wrapper around Firebase Firestore's unwieldy query.addSnapshotListener functionality, in case you were wondering. Using modern Result type instead of a callback with multiple optional parameters.)
In an older project I am using ReactiveKit and have an extension that turns all this into a Signal, like so:
extension QueryObserver {
public static func asSignal(query: Query) -> Signal<[T], Error> {
return Signal { observer in
let queryObserver = QueryObserver<T>(query: query) { result in
switch result {
case .success(let value):
observer.receive(value)
case .failure(let error):
if let firestoreError = error as? FirestoreError, case .noSnapshot = firestoreError {
observer.receive([])
} else {
observer.receive(completion: .failure(error))
}
}
}
return BlockDisposable {
queryObserver.stopListening()
}
}
}
}
In a brand new project though, I am using Combine and am trying to rewrite this. So far as I have managed to write this, but it doesn't work. Which makes sense: the observer is not retained by anything so it's immediately released, and nothing happens.
extension QueryObserver {
public static func asSignal(query: Query) -> AnyPublisher<[T], Error> {
let signal = PassthroughSubject<[T], Error>()
let observer = QueryObserver<T>(query: query) { result in
switch result {
case .success(let value):
print("SUCCESS!")
signal.send(value)
case .failure(let error):
if let firestoreError = error as? FirestoreError, case .noSnapshot = firestoreError {
signal.send([])
} else {
signal.send(completion: .failure(error))
}
}
}
return signal.eraseToAnyPublisher()
}
}
How do I make the Combine version work? How can I wrap existing async code? The only examples I found used Future for one-off callbacks, but I am dealing with multiple values over time.
Basically I am looking for the ReactiveKit-to-Combine version of this.
Check out https://github.com/DeclarativeHub/ReactiveKit/issues/251#issuecomment-575907641 for a handy Combine version of a Signal, used like this:
let signal = Signal<Int, TestError> { subscriber in
subscriber.receive(1)
subscriber.receive(2)
subscriber.receive(completion: .finished)
return Combine.AnyCancellable {
print("Cancelled")
}
}
I'm slowly getting my head around completion handlers.
Kind of working backwards if I have a firestore query if I wanted to use a completion handler i'd have to use completion() when the firestore query finishes.
But it's setting up the function that still confuses me.
So if this is a function definition that takes a closure as a parameter:
func doSomethingAsync(completion: () -> ()) {
}
I don't quite get how to go from the above func definition and implementing it for something real like a firestore query and request.
query.getDocuments(){ (querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)")
}
else
{
if(querySnapshot?.isEmpty)!
{
print("there's no document")
completion()
}
else
{
for document in querySnapshot!.documents
{
completion()
}
}
}
}
thanks.
update
so for my example could i do something like
func getFirestoreData(userID: String, completion #escaping() -> ()){
//firestore code:
query.getDocuments(){ (querySnapshot, err) in
if let err = err
{
print("executed first")
completion()
}
else
.......
print("executed first")
completion()
}
}
To call the function i'm doing:
getFirestoreData(userID: theUserID) {
print("executes second")
}
print("executes third") after function execution.
What i'd like to happen is the programming awaits the completion() before continuing to execute.
But "executes third" happens first, then "executes first", then "executes second".
Thanks
Here is full example (With API Call)
Note that : status variable is just a key to finger out what is response from server
(0: error from server, 1: success, -1: something wrong in my code)
func logout(handlerBack: #escaping (_ error: Error?, _ status:Int?, _ error_message:String?)->())
{
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)
.responseJSON { respons in
switch respons.result {
case .failure(let theError):
handlerBack(theError, 0, nil)
case .success(let data):
let json_data = JSON(data)
/// if couldn't fetch data
guard let status = json_data["status"].int else {
handlerBack(nil,-1, "can't find status")
return
}
/// if statuse == 0
guard status == 1 else {
handlerBack (nil, 0 , json_data["errorDetails"].string)
return
}
// that's means everything fine :)
handlerBack(nil, 1 , nil)
}
}
}
And here is the way to implement it :
// call func
self.logout { (error:error, status:Int, error_message:String) in
// if status not 1, figure out the error
guard status == 1 else {
// try to find error from server
guard let error_message = error_message else {
// else error something else
print ("Error at :: \(#function)")
// don't do anything ..just return
return
}
self.showMessageToUser(title: error_message, message: "", ch: {})
return
}
// this part do what ever you want, means every thing allright
}
UPDATE :
You are looking for something wait unit execute "First" and "Second"
in this case use DispatchGroup() here is the example :
var _dispatch_group = DispatchGroup()
getFirestoreData(userID: theUserID) {
_dispatch_group.enter()
print("executes second")
_dispatch_group.leave()
}
_dispatch_group.notify(queue: .main) {
print("executes third")
}
output is :
executes First
executes Second
executes Third