SQLite.swift
SQLite.swift copied to clipboard
Noob question, how to load custom extension?
Amazing library.
I have a question. I wanted to use this extension (embedding for SQLite). How can I use it with SQLite.swift?
https://github.com/asg017/sqlite-vss
Thanks,
Hi,
there's no direct support for loading extensions in the library. You'll need to compile sqlite-vss in your project and then initialize the extension(s) when your app starts, for example from main.m
#include "sqlite3.h"
#include "sqlite-vector.h"
#include "sqlite-vss.h"
int main(int argc, char **argv) {
sqlite3_auto_extension((void (*)(void)) sqlite3_vector_init);
sqlite3_auto_extension((void (*)(void)) sqlite3_vss_init);
// start actual iOS app
UIApplicationMain(argc, argv, nil, NSStringFromClass(AppDelegate.class));
}
I haven't tested this, but this in general how you load SQLite extensions.
See the sample code here: https://github.com/asg017/sqlite-vss/blob/c3bacfd61ed122a581942108ee1e457767f28876/examples/c/demo.c#L28
How can that solution be adapted to a Swift project? Looks like main.m
doesn't exist in that context 🤷♂️
You can create a main.m
in even for a Swift project. Perhaps an easier solution could be to use the @main
annotation in Swift, but I haven't tried this.
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/#main
Ty. Actually eventually figured it out (I'm an extension author not iOS dev 😅) --
https://github.com/tantaman/xcode-starter/blob/a755ad175caf091b3e61d4aa0fe69e996082ecf4/xcode-starter/xcode_starterApp.swift#L10-L14
The new wrinkle is this:
SQLITE_DEPRECATED_NO_REPLACEMENT("Process-global auto extensions are not supported on Apple platforms", macos(10.10, 10.10), ios(8.2, 8.2), watchos(2.0, 2.0), tvos(9.0, 9.0))
SQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
so sqlite3_auto_extension
always returns an error.
We've worked around that problem by using the sqlite3
package from cocoapods