1

I'm pretty new to Swift, more specifically on Kitura. I'm using SwiftKueryPostgresql to handle communication with database.

I'm writting an ORM library for a specific project. I'm testing my connection like this (as I understood from documentation) :

import Foundation
import SwiftKuery
import SwiftKueryPostgreSQL
import XCTest
@testable import myprojet

final class project_ormTests: XCTestCase {
    var context: Context? = Context(pool: PostgreSQLConnection.createPool(host: "localhost", port: 5432, options: [.databaseName("project"), .userName("myproject"), .password("")], poolOptions: ConnectionPoolOptions(initialCapacity: 3)).getConnection())
    }

    func testConnection() {

        XCTAssertNotNil(context)
    }

    static var allTests = [
        ("testConnection", testConnection),
    ]
}

My class Context :

public class Context {
    private let pool : Connection

    public init?(pool : Connection?) {
        guard pool != nil else { return nil }
        self.pool = pool!
    }
}

When I run testConnection() on xcode, I reach SwiftKuery/ConnectionPool.swift on line 129 with this error

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 

What am I doing wrong ? Is there something I missed ? I really have no idea what to do...

Here's my Package.swift if it can help

import PackageDescription

let package = Package(
    name: "project",
    products: [
        .library(
            name: "projet",
            targets: ["project"]),
    ],
    dependencies: [
        .package(url: "https://github.com/IBM-Swift/Swift-Kuery-PostgreSQL.git", from: "1.2.0")
    ],
    targets:[
        .target(
            name: "project",
            dependencies: ["SwiftKueryPostgreSQL"]),
        .testTarget(
            name: "projectTests",
            dependencies: ["project"]),
    ]
)

Thank you

Zyigh
  • 425
  • 4
  • 16

2 Answers2

1

The Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error normally happens when force unwrapping and optional which is set to nil. I would suggest instead of doing self.pool = pool!, you can do guard self.pool = pool else { return nil } The guard statement will unwrap the optional value for you and if it can't if will fall to the else.

A few things I noticed: - In your Package.swift the SwiftKuery dependency is missing. - It seems you have a typo in testable import myprojet

The line the error is pointing to is the deinit function for the Connection. This function gets called when the Connection is out of scope or not referenced by another object to clear the memory. You may be trying to access the Connection after it has been deleted thus getting an error throned.

Enrique
  • 26
  • 2
  • Thanks for your answer. I did the changes you said (and seemed very justified) and now I have the following error : `value of type 'Connection? in assignment' does not conform to 'Connection'` which is pretty confusing – Zyigh Jul 05 '18 at 14:10
  • About SwiftKuery, as strange as it seems, I don't have to put it in my Package.swift because it's imported by SwiftKueryPostgresql, but at some point I have to import it explicitly to use `Connection` for example – Zyigh Jul 05 '18 at 14:16
  • 1
    So Swift-Kuery is an abstraction layer of our SQL Plugins. In this case, `Connection` protocol is declared in `Swift-Kuery` and used in `Swift-Kuery-PostgreSQL` but it does not expose it to the top level application. – Enrique Jul 05 '18 at 14:28
  • 1
    The error is caused because you are trying to assign a type of `Connection?` to `Connection` - You first have to unwrap it. `guard let unwrapped = pool else {return nil}` and then assign it `self.pool = unwrapped` – Enrique Jul 05 '18 at 14:29
  • Ok thanks you helped me a lot. I'm very confused with strongly typed languages as I come from PHP. Hope I'll get better soon. Thanks a lot – Zyigh Jul 05 '18 at 23:22
0

I suggest you to learn how guard let works in swift but here is basically what you need to do:

public class Context {
    private let pool : Connection

    public init?(pool : Connection?) {
        guard let poolThatIsNotNil = pool else { return nil }
        self.pool = poolThatIsNotNil
    }
}
Fangming
  • 24,551
  • 6
  • 100
  • 90