Start a new topic

Asynchronous function Query


In order to not be repeating the code everywhere I need to implement an object that returns a string from a query. How can I make the function to wait until the data is available?

The below code gives an error but if I move the return response after the query call the it will always return "" because it returns the value before the function has queried the values :( 


import Foundation

import Kinvey

class DataManager {


    var books=[filesMapping]();

    var fileName:String = "";


    func getIdforFile( )  -> String {

        var response :String = "";

        var responded:Bool = false;

        let dataStore = DataStore<filesMapping>.collection(.network)

        let predicateCompany = NSPredicate(format: "company == %@", "refreshments SA")

        let predicateElement = NSPredicate(format: "element == %@", "character")

        let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [predicateCompany, predicateElement])

        let query = Query(predicate: predicate)


        dataStore.find(query   ) { books, error in

            if let books = books {


                print("Found file mapping");

                response = books[0].fileID!;

                return response;

            } else {

                response = "Error";

                return response;







Have you tried to print the error object? What error are you getting?

Also, can you update the SDK and try the same code with latest Kinvey SDK v3.5.0? You might need to update Swift version for the same.



Kinvey Support


There is no error message. The function executes normally. What I need is a handler or something so the function waits until the file is downloaded to return the value. 

Hello Emillio,

We are continuing to investigate this problem and I have just reached out to Engineering.

In the second paragraph of the problem description you make the following statement.

"The below code gives an error..."

Can you tell us what the error is that you are seeing. You said in your last update that there is no error message. If you are seeing an error, then what is the error, what happens?


Billy Gee

Login or Signup to post a comment