Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Invalid conversion from throwing function type

Tags:

ios

swift

I'm new to swift and
I'm not understanding why I'm getting this error even doing the do catch treatment.

I've being reading similar questions and so far none of them solved this error :
Invalid conversion from throwing function type '(_) throws -> (). to non-throwing function type '([BeaconModel]) -> ()' at the line BeaconModel.fetchBeaconsFromRestApi(completionHandler: { .....

The piece of code with the error:

do{

    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let db = try Connection("\(path)/db.sqlite3")

    let beaconsTbl = Table("beacons")
    let id = Expression<Int64>("id")
    let uuid = Expression<String>("uuid")
    let major = Expression<String>("major")
    let minor = Expression<String>("minor")

    try db.run(beaconsTbl.create { t in
        t.column(id, primaryKey: true)
        t.column(uuid)
        t.column(major)
        t.column(minor)
    })

    BeaconModel.fetchBeaconsFromRestApi(completionHandler: {
        beacons in
        for item in beacons{

            let insert = beaconsTbl.insert(id <- item.id!, uuid <- item.uuid!, major <- item.major!, minor <- item.minor!)
            try db.run(insert)
        }
    })

} catch {
    print("Error creating the database")
}

The fetch method:

static func fetchBeaconsFromRestApi( completionHandler: @escaping (_ beacons: [BeaconModel]) -> ()){

    Alamofire.request(Constants.Beacons.URLS.ListAllbeacons).responseArray(keyPath: "data") { (response: DataResponse<[BeaconModel]>) in

        let beaconsArray = response.result.value
        if let beaconsArray = beaconsArray {
            completionHandler(beaconsArray)
        }
    }
}

It uses Alamofire and AlamofireObjectMapper. Can you see what I'm missing?

Thanks for any help

like image 739
André Luiz Avatar asked Oct 03 '16 20:10

André Luiz


1 Answers

completionHandler in fetchBeaconsFromRestApi should not throw. So you should wrap all throwing calls with do - catch:

BeaconModel.fetchBeaconsFromRestApi(completionHandler: {
    beacons in
    for item in beacons{

        let insert = beaconsTbl.insert(id <- item.id!, uuid <- item.uuid!, major <- item.major!, minor <- item.minor!
        do {
            try db.run(insert)
        } catch {
            print("Error creating the database")
        }
    }
})
like image 52
Avt Avatar answered Oct 17 '22 06:10

Avt