How do I connect my swift code in Xcode on a mac with a MYSQL or mongoDB server

I have been having the most insane issue of trying to connect the latest xcode swift project with mysql server or even mongodb.....I have check most libraries on github and stuff and nothing worked, and yes I have check this forum out too and I was left with no choice but to post something here. So please help! Thank you

Are you talking about an iOS/macOS app? If so, then it's not surprising that no library has support for that use-case, as that is not something you generally should do. Except in special cases, your app should talk to a backend that in turn connects to your database.

Aside from @ahti's quesions, do you have any errors and/or code that you can share?I maintain the MongoKitten (MongoDB) driver, and have frequently worked on the vapor MySQL driver. Most of the people that work with these libraries are in slack teams (MongoKitten for example) and/or the Vapor discord.

Talking about IOS app, how would I go about connecting to a database to start with? I cant even seem to find how and were to start

While the sentiment is right, that usecase should work w/ pretty much any library available. E.g. Direct2SwiftUI even does that on a watch ;-)

This one is easy to use: https://github.com/codewinsdotcom/PostgresClientKit

But I don't know of a nice tutorial for this specific thing either.

This is true, MongoKitten works on iOS (even through Network.framework). The Vapor drivers don't support Network.framework but that still works on WiFi.

Aside from that, I highly recommend not connecting directly to a database from your app unless it's an extremely limited usergroup (your company's IT department that should already have access). Or unless you're building a database browser.

Yes I am getting alot of errors for Vapor:
'openssl/conf.h' file not found
and three of these
image

And for mongoKitten, I got errors:
Currently this is what I am doing to add this to my Xcode project:
Go to Xcodeproject file
select Swift Package
Add https://github.com/OpenKitten/MongoKitten.git to the repo url
then select version 6.0.0
and then select mongo kitten and add
then I import MongoKitten in my ViewController

You'll need a different MongoKitten branch, because I didn't want to tag the iOS build yet so I can make breaking changes that I never ended up doing.

EDIT: The branch is mobile/6.0. I can add a prerelease tag for the iOS build if you're eager to use it.

Whats the general architecture of IOS application trying to access the database? How is it usually done? My current use case:
I have an app that logs current headcount and the user name into a database and another user anywhere around the world should be able to use the username and be able to see the headcount of that particular user

Most apps, iOS or not, have or create a separate application as a server. This server, the API, takes care of authentication and authorization. It exposes an interface, most commonly using HTTP and JSON, that the apps connect to. After authenticating with the app to the API, the app retains a piece of proof like a JWT token that proves that this app is used by said user. This token is then sent with all other requests, such as getting the headcount or updating the headcount. This proves that the user doing asking for information or updating it is allowed to do so. The API not only provides these interfaces, but also checks whether the attempted access is permitted. This API, in turn, connects with the database.

If an iOS app that's published in the app store connects directly to a database, people can scan the app's binary for the credentials of the database. I'd be surprised if there aren't automated scans for this.

Vapor and MongoKitten are Swift projects, but they're mainly used to build APIs. Because the libraries are built in Swift, the same iOS developers that build the app can use their existing knowledge and experience in Swift to build the API. This way one developer could do it all, and often even reuse code.

NIO-ELT-0-#0 (9): Precondition failed: lock() failed in pthread_mutex with error 11
I also get this error from mongodb after like 5 mins

let err = pthread_mutex_lock( self .mutex)

precondition(err == 0, "( #function ) failed in pthread_mutex with error (err)")

Precondition failed: lock() failed in pthread_mutex with error 11: file ( SourcePackages/checkouts/swift-nio/Sources/NIOConcurrencyHelpers/lock.swift)

Do you happen to be using breakpoints before you get that? I've had that issue only when using breakpoints so far, but it's very annoying for sure.

As an added note, this should only be happening if you're using .wait() to get the results. Ideally you wouldn't be using .wait() because it will significantly harm application performance.

Not really a break point but its like it timesout while running which is odd...But im gonna build a whole new server and new mongo instance and retry right now in a few

Before I continue, I do still want to strongly suggest using a separate API.


Did you already set up a MongoDB atlas account? And if so, did you use the connection string they provided for the MongoDB client (adding your own credentials)?

I do plan on using a API in the future, the API usually takes time to build and I dont have time for that as of now due to current situation I am in and the current world situation hahah I used mongodb+srv://username:password@cluster0-gbmif.mongodb.net/test

Ofcourse I put my user and password in

:+1: Could you try adding ?authSource=admin to the connection string? If that didn't help, is there an error being thrown by MongoKitten?

Current code:
do{
let client = try MongoDatabase.synchronousConnect("mongodb+srv://user:pass@cluster0-gbmif.mongodb.net/test?retryWrites=true&w=majority")
}catch{
print(error)
}

Current Error:
2020-04-29 07:11:18.362765-0500 testings[13666:134416] [si_destination_compare] send failed: Invalid argument

2020-04-29 07:11:18.362923-0500 testings[13666:134416] [si_destination_compare] send failed: Undefined error: 0

after adding:
?authSource=admin to the end

same error

The error that you're getting is usually more of a warning. This is caused by the DNS client.

If you're appending authSource like this,the connecting string becomes invalid:
mongodb+srv://user:pass@cluster0-gbmif.mongodb.net/test?retryWrites=true&w=majority?authSource=admin

Do it like this:
mongodb+srv://user:pass@cluster0-gbmif.mongodb.net/test?retryWrites=true&w=majority&authSource=admin

If you did do the second one and it still didn't work, you can try specifying a custom DNS Server using &dnsServer=8.8.8.8

1 Like
Terms of Service

Privacy Policy

Cookie Policy