Function returns null value - swift

This might be a basic question but I am unable to figure this out. My function always returns a null value and I am not sure why.
My main code ( in a different view controller) calls this function with a "string" to search. But at the end of the function, I can never return the results. If i print the search results where I have the final print statement, then I do see the results. But right after that function is complete, the return value is nil. How would I get the return value from this function ? Thanks in advance,. I am using Swift btw.
func fetchSearchResults (_ searchString: String) -> Array<Any> {
self.globalPost = [Posts]()
self.postKeysArray = []
var searchIndex:AlgoliaSearch.Index
let query = Query()
searchIndex = self.client.index(withName: "Comments")
// you can ignore all of this below until the last couple lines...
query.attributesToRetrieve = ["userComment","postId"]
query.attributesToHighlight = ["postId"]
query.query = searchString
query.optionalWords = [searchString]
let curSearchId = searchId
searchIndex.search(query, completionHandler: { (content, error) in
if (curSearchId <= self.displayedSearchId) || (error != nil) {
return // Newest query already displayed or error
}
self.displayedSearchId = curSearchId
self.loadedPage = 0 // Reset loaded page
guard let hits = content!["hits"] as? [[String: AnyObject]] else { return }
var tmp = [String]()
for hit in hits {
// print ("Comment is \(commentRecord(json:hit).postId)")
tmp.append(commentRecord(json:hit).postId!)
}
self.postKeysArray = tmp
self.getDatafromFirebase(searchString)
// so my search code returns some values here and i see them in the post keysArray.
print ("Search Results -post keys is \(self.postKeysArray)") \\ returns correct values here in the Array
})
self.searchId += 1
// But here i get a NULL value.
return self.postKeysArray \\ returns empty array
}

As Alexander noted, you are printing the search results inside the completion handler, which has not executed yet when fetchSearchResults returns. There are a couple different ways that you could solve this. One would be to refactor fetchSearchResults to take a closure:
func fetchSearchResults (_ searchString: String, completionHandler completion: (Array<Any>) -> ()) {
// ...
searchIndex.search(query, completionHandler: { (content, error) in
// ...
self.postKeysArray = tmp
self.getDatafromFirebase(searchString)
// so my search code returns some values here and i see them in the post keysArray.
print ("Search Results -post keys is \(self.postKeysArray)") \\ returns correct values here in the Array
completion(self.postKeysArray)
})
}
Then, in your 'main code', wherever you were calling:
let results = searchController.fetchSearchResults(searchString)
doSomething(with: results)
You would now have:
searchController.fetchSearchResults(searchString, { results in
doSomething(results)
})
This line could also be written:
searchController.fetchSearchResults(searchString, doSomething)
If you really can't have fetchSearchResults be asynchronous, then you could use DispatchSemaphore to wait until the completion handler has finished:
func fetchSearchResults (_ searchString: String) -> Array<Any> {
// ...
var semaphore = DispatchSemaphore(0)
searchIndex.search(query, completionHandler: { (content, error) in
// ...
self.postKeysArray = tmp
self.getDatafromFirebase(searchString)
// so my search code returns some values here and i see them in the post keysArray.
print ("Search Results -post keys is \(self.postKeysArray)") \\ returns correct values here in the Array
semaphore.signal()
})
semaphore.wait()
return self.postKeysArray
}
Note that unless you know what you're doing, using semaphores is not a good idea. If you don't know what threads various callbacks might be called on, it can lead to a deadlock. The above example will not work if the searchIndex.search completion handler is called on the same DispatchQueue as fetchSearchResults. If you don't understand what I'm talking about, you probably shouldn't do it. It will almost certainly be better to use asynchronous callbacks.

Related

Why is My Code running this way and is There a better way to solve this issue

// In the code below I am trying to return an array from data in firestore, the array always returned empty when I put the handler outside the for loop so I had to use an if statement inside the for loop to get the array containing the data. after using the print statement you see in the code i found out that the compiler is going over the entire function before entering the for loop, (print("5") & (print("6") are the first to run and when I put the handler outside the for it will also be triggered and return an empty array
**
func getMyGames(joinedGamesIDs: [String], handler: #escaping(_ games: [GameViewModal]) -> ()) {
var games = [GameViewModal]()
if !joinedGamesIDs.isEmpty{
for id in joinedGamesIDs {
db.collection("games").document(id).getDocument { (document, error) in
if let document = document, document.exists {
if let game = self.getGameViewModal(document: document){
games.append(game)
print("1")
print(games.count)
}
print("2")
print(games.count)
}
print("3")
print(games.count)
if games.count == (joinedGamesIDs.count){
handler(games)
}
print("4")
print(games.count)
}
}
print("5")
print(games.count)
}
print("6")
print(games.count)
}
**
I've embedded my explanations in the code commentary for easier reading. But the problem you have is that you aren't coordinating these async tasks (the getting of each document). You must coordinate them so when the last one finishes, you can "return" the array from the function. This function doesn't technically "return" anything (except Void) but the completion handler, in a way, "returns" the array which is why I put it in quotes. These semantic details matter and it helps to understand everything better.
func getMyGames(joinedGamesIDs: [String], handler: #escaping (_ games: [GameViewModel]) -> ()) {
guard !joinedGamesIDs.isEmpty else {
// If there is nothing to do, always consider
// calling the handler anyway, with an empty
// array, so the caller isn't left hanging.
return handler([])
}
// Set up a Dispatch Group to coordinate the multiple
// async tasks. Instatiate outside of the loop.
let group = DispatchGroup()
var games: [GameViewModel] = []
for id in joinedGamesIDs {
// Enter the group on each iteration of async work
// to be performed.
group.enter()
db.collection("games").document(id).getDocument { (document, error) in
if let doc = document,
doc.exists,
let game = self.getGameViewModal(document: doc) {
games.append(game)
} else if let error = error {
// Always print errors when in development.
print(error)
}
// No matter what happens inside the iteration,
// whether there was a success in getting the
// document or a failure, always leave the group.
group.leave()
}
}
// Once all of the calls to enter the group are equalled
// by the calls to leave the group, this block is called,
// which is the group's own completion handler. Here is
// where you ultimately call the function's handler and
// return the array.
group.notify(queue: .main) {
handler(games)
}
}

Querying and mapping using Vapor

I'm trying to write a function using Swift and Vapor but I don't understand why one statement gets printed before the other:
// Logout user
func logout(_ req: Request) throws -> Future<APIResponseMessage> {
let userID = self.checkAccessToken(req: req)
// Delete access token here
let apiResponseMessage = APIResponseMessage()
apiResponseMessage.message = "success"
apiResponseMessage.userID = userID
return apiResponseMessage.create(on: req)
}
func checkAccessToken(req: Request) -> Int {
let bearerAuthorization = req.http.headers.bearerAuthorization
guard let _bearerAuthorization = bearerAuthorization else {
// Works fine
print("no bearer incluced")
return 0
}
let _ = AccessToken.query(on: req).filter(\.accessToken == _bearerAuthorization.token).first().map(to: Int.self) { queriedAccessToken in
// This should be first
print("This gets printed second")
return queriedAccessToken!.userID!
}
// This should be second
print("This gets printed first")
return 0
}
Can anyone tell me how to make the second print statement wait until the first one is completed?
Right now it's causing my logout function to run with userID == 0 when this shouldn't be the case
As #nathan said, this is due to your code being async. Your .map callback is like the closure you pass into to a URLSession.dataTask when making request's to an external API for an iOS app.
Vapor uses a slightly different async model then what you use in iOS though, using promises and futures instead of callback closures. You can read about them in the docs.
In your case, you want to return the userID you get from the AccessToken query. To do this, you first need to change your method's return type from Int to Future<Int>. Then, instead of assigning the result of the .map call to _, you can return it from the method:
func checkAccessToken(req: Request) -> Future<Int> {
let bearerAuthorization = req.http.headers.bearerAuthorization
guard let _bearerAuthorization = bearerAuthorization else {
return req.future(0)
}
return AccessToken.query(on: req).filter(\.accessToken == _bearerAuthorization.token).first().map(to: Int.self) { queriedAccessToken in
return queriedAccessToken!.userID!
}
}
I would suggest you look into error handling for your queriedAccessToken and userID values so you aren't force-unwrapping them.

Can't get data returned from dataTask()

For one week I have been trying to get a string returned from dataTask().
I already read a lot here on StackOverFlow and also from serval sites where they tackle this topic. For example, this one. So I already understand that it's that the dataTask doesn't directly return values, cause it happens on different threads and so on. I also read about closures and completion handlers. I really got the feeling that I actually already got a little clue what this is about. But I can't get it to work.
So this is my code. I just post the whole code so no-one needs to worry that the problem sticks in a part which I don't show. Everything is working fine until I try to return a value and save it for example in a variable:
func requestOGD(code gtin: String, completion: #escaping (_ result: String) -> String) {
// MARK: Properties
var answerList: [String.SubSequence] = []
var answerDic: [String:String] = [:]
var product_name = String()
var producer = String()
// Set up the URL request
let ogdAPI = String("http://opengtindb.org/?ean=\(gtin)&cmd=query&queryid=400000000")
guard let url = URL(string: ogdAPI) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print("error calling GET on /todos/1")
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result, which is String. It willbecome split and placed in a dictionary
do {
let answer = (String(decoding: responseData, as: UTF8.self))
answerList = answer.split(separator: "\n")
for entry in answerList {
let entry1 = entry.split(separator: "=")
if entry1.count > 1 {
let foo = String(entry1[0])
let bar = String(entry1[1])
answerDic[foo] = "\(bar)"
}
}
if answerDic["error"] == "0" {
product_name = answerDic["detailname"]!
producer = answerDic["vendor"]!
completion(product_name)
} else {
print("Error-Code der Seite lautet: \(String(describing: answerDic["error"]))")
return
}
}
}
task.resume()
Here I call my function, and no worries, I also tried to directly return it to the var foo, also doesn't work The value only exists within the closure:
// Configure the cell...
var foo:String = ""
requestOGD(code: listOfCodes[indexPath.row]) { (result: String) in
print(result)
foo = result
return result
}
print("Foo:", foo)
cell.textLabel?.text = self.listOfCodes[indexPath.row] + ""
return cell
}
So my problem is, I have the feeling, that I'm not able to get a value out of a http-request.
You used a completion handler in your call to requestOGD:
requestOGD(code: listOfCodes[indexPath.row]) {
(result: String) in
// result comes back here
}
But then you tried to capture and return that result:
foo = result
return result
So you're making the same mistake here that you tried to avoid making by having the completion handler in the first place. The call to that completion handler is itself asynchronous. So you face the same issue again. If you want to extract result at this point, you would need another completion handler.
To put it in simple terms, this is the order of operations:
requestOGD(code: listOfCodes[indexPath.row]) {
(result: String) in
foo = result // 2
}
print("Foo:", foo) // 1
You are printing foo before the asynchronous code runs and has a chance to set foo in the first place.
In the larger context: You cannot use any asynchronously gathered material in cellForRowAt. The cell is returned before the information is gathered. That's what asynchronous means. You can't work around that by piling on further levels of asynchronicity. You have to change your entire strategy.

Result of call is unused

Right below the second comment, I receive an error of "Result of call to 'taskForDeleteMethod' is unused. Why is this when I use the results and error in the closure following the call?
func deleteSession(_ completionHandlerForDeleteSession: #escaping (_ success: Bool, _ error: NSError?) -> Void) {
/* 1. Specify parameters, method (if has {key}), and HTTP body (if POST) */
// There are none...
/* 2. Make the request */
taskForDELETEMethod { (results, error) in
/* 3. Send the desired value(s) to completion handler */
if let error = error {
print("Post error: \(error)")
completionHandlerForDeleteSession(false, error)
} else {
guard let session = results![JSONKeys.session] as? [String: AnyObject] else {
print("No key '\(JSONKeys.session)' in \(results)")
return
}
if let id = session[JSONKeys.id] as? String {
print("logout id: \(id)")
completionHandlerForDeleteSession(true, nil)
}
}
}
}
In earlier swift versions, you need not bother about the return value of a method. You may store it in any variable snd use it later or you may ignore it completely. Neither it gave any error nor a warning.
But in swift 3.0 you need to specify whether you want to ignore the returned value or use it.
1. If you want to use the returned value, you can create a variable/constant and store the value in it, i.e
let value = taskForDELETEMethod {
// Your code goes here
}
2. If you want to ignore the returned value, you can use _ ,i.e
let _ = taskForDELETEMethod {
// Your code goes here
}
You are confusing the results variable, which is, indeed, used inside the closure, and the result of the taskForDELETEMethod call itself, which is NSURLSessionDataTask object.
From the examples of using taskForDELETEMethod that I was able to find online it looks like it is perfectly OK to ignore the return value, so you can avoid this warning by assigning the result to _ variable, i.e.
let _ = taskForDELETEMethod {
... // The rest of your code goes here
}

How to return values from Haneke's async fetch method

I'm trying to parse some data that I cached using Haneke Swift. I've cached the data and have written the parser to accomplish this. This parser is in a separate class called AssembleCalendar().
Using Haneke's example code for fetching, I've tried with complete and utter failure to actually return a value from the closure.
My attempt
func getScheduledItems() -> [ScheduledItem] {
var scheduledItem = [ScheduledItem]() // initialize array
let cache = Shared.dataCache
cache.fetch(key: "CalendarData").onSuccess { data in
scheduledItem = AssembleCalendar().assimilate(data) // update array
print(scheduledItem) // array returns expected value
}
print(scheduledItem) // array returns nil
return scheduledItem // returns nil
}
What I know
I understand that this is an asynchronous issue. My code isn't waiting for my AssembleCalendar() parser to finish. It's just running each line and returns nil long before my scheduledItem receives a value. I've tried many, many solutions and read quite a few examples online but I cannot figure out how to retrieve a value from this closure in this scenario.
My question
How can I get .fetch() to return a value before my function hits nil?
update:
Here's my code in context:
class Schedule {
var items : [ScheduledItem]
init() {
items = getScheduledItems() // Schedule.getScheduledItems()
}
func getScheduledItems(completion: (items: [ScheduledItem]) -> ()) {
var scheduledItem = [ScheduledItem]() // initialize array
let cache = Shared.dataCache
cache.fetch(key: "CalendarData").onSuccess { data in
scheduledItem = AssembleCalendar().assimilate(data) // update array
print(scheduledItem) // array returns expected value
completion(items: scheduledItem)
}
}
}
Fetch() is using a completion handler, this means that the block of code called in there is actually executing AFTER your return statement has been executed. This is why it is returning nil. To get around this, you can use your own completion handler to return the information:
class Schedule {
var items = [ScheduledItem]()
init(items: [ScheduledItem]) {
self.items = items
}
class func getScheduledItems(completion: (items: [ScheduledItem]) -> ()) {
var scheduledItem = [ScheduledItem]() // initialize array
let cache = Shared.dataCache
cache.fetch(key: "CalendarData").onSuccess { data in
scheduledItem = AssembleCalendar().assimilate(data) // update array
print(scheduledItem) // array returns expected value
completion(items: scheduledItem)
}
}
}
Then initialise the class using this:
Schedule.getScheduledItems { (items) -> () in
var schedule = Schedule(items: items)
}