Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLDelight does not work with tvOS #1668

Closed
Thomas-Vos opened this issue Apr 28, 2020 · 2 comments
Closed

SQLDelight does not work with tvOS #1668

Thomas-Vos opened this issue Apr 28, 2020 · 2 comments

Comments

@Thomas-Vos
Copy link

SQLDelight has the tvOS target but it does not work. Apple TV apps cannot store files apart from the cache directory (which can be deleted automatically).

Check out "Local Storage for Your App Is Limited" at: https://developer.apple.com/library/archive/documentation/General/Conceptual/AppleTV_PG/index.html

Note that this limitation is only for a real Apple TV. For some reason the simulator is allowed to store files (looks like a bug).

When running SQLDelight on a real Apple TV it shows the following error:

2020-04-28 12:50:21.254856+0200 HueEssentialsTv[213:4013] [logging-persist] cannot open file at line 43353 of [378230ae7f]
2020-04-28 12:50:21.254921+0200 HueEssentialsTv[213:4013] [logging-persist] os_unix.c:43353: (0) open(/var/mobile/Containers/Data/Application/7412B405-7AC7-4C53-9B1D-AAD60C5F9311/Library/Application Support/databases/HueEssentials.db) - Undefined error: 0
Exceptions are propagated from Kotlin to Objective-C/Swift as NSError only if method has or inherits @Throws annotation
Uncaught Kotlin exception: kotlin.Exception: android/database/sqlite/SQLiteCantOpenDatabaseException - unknown error (code 14): Could not open database
        at 0   common                              0x000000010166aee0 ThrowSql_SQLiteException + 220
        at 1   common                              0x0000000102154d7c _ZN7android23throw_sqlite3_exceptionEiPKcS1_ + 656
        at 2   common                              0x0000000102154e68 SQLiter_SQLiteConnection_nativePrepareStatement + 0
        at 3   common                              0x00000001021550ac SQLiter_SQLiteConnection_nativeOpen + 328
        at 4   common                              0x000000010166d7e8 kfun:co.touchlab.sqliter.NativeDatabaseManager.createConnection#internal + 360
        at 5   common                              0x000000010166d2dc kfun:co.touchlab.sqliter.NativeDatabaseManager.createMultiThreadedConnection()co.touchlab.sqliter.DatabaseConnection + 112
        at 6   common                              0x00000001016701c8 kfun:com.squareup.sqldelight.drivers.native.NativeSqliteDriver.$<init>$lambda-0$FUNCTION_REFERENCE$394.invoke#internal + 184
        at 7   common                              0x0000000101673d00 kfun:com.squareup.sqldelight.drivers.native.SinglePool.<init>(kotlin.Function0<T>)com.squareup.sqldelight.drivers.native.SinglePool<T> + 384
        at 8   common                              0x0000000102126054 objc2kotlin.21904 + 5564
        at 9   HueEssentialsTv                     0x0000000100bf1e50 $s15HueEssentialsTv4DepsC11bridgeModelSo012CommonBridgeF0_pvg + 148
        at 10  HueEssentialsTv                     0x0000000100be2f8c $s15HueEssentialsTv15GroupsViewModelCACycfcSo06CommondeF0CycfU_ + 160
        at 11  HueEssentialsTv                     0x0000000100c1841c $s15HueEssentialsTv13BaseViewModelC2ktxvg + 48
        at 12  HueEssentialsTv                     0x0000000100c7da98 $s15HueEssentialsTv11LceViewImpl33_57550377B1AA6BC892F432DF3F43937BLLV4bodyQrvg + 52
        at 13  HueEssentialsTv                     0x0000000100c7f400 $s15HueEssentialsTv11LceViewImpl33_57550377B1AA6BC892F432DF3F43937BLLVyxq_q0_q1_q2_G7SwiftUI0E0AafGP4body4BodyQzvgTW + 40
        at 14  SwiftUI                             0x00000001cf700af4 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 961268
        at 15  SwiftUI                             0x00000001cf703208 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 971272
        at 16  AttributeGraph                      0x00000001d10a4434 F506D8B1-2D6A-3651-97C6-4ED8B885DA9F + 119860
        at 17  AttributeGraph                      0x00000001d108d718 F506D8B1-2D6A-3651-97C6-4ED8B885DA9F + 26392
        at 18  AttributeGraph                      0x00000001d108dbd0 F506D8B1-2D6A-3651-97C6-4ED8B885DA9F + 27600
        at 19  AttributeGraph                      0x00000001d109294c F506D8B1-2D6A-3651-97C6-4ED8B885DA9F + 47436
        at 20  SwiftUI                             0x00000001cf7e870c 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 1910540
        at 21  SwiftUI                             0x00000001cf7e8a54 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 1911380
        at 22  SwiftUI                             0x00000001cf7e87c0 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 1910720
        at 23  SwiftUI                             0x00000001cfa6f4cc 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 4560076
        at 24  SwiftUI                             0x00000001cfa6efcc 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 4558796
        at 25  SwiftUI                             0x00000001cfa65a88 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 4520584
        at 26  SwiftUI                             0x00000001cfb288dc 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 5318876
        at 27  SwiftUI                             0x00000001cfb28908 1DB3700E-5788-37B9-B4B5-3A99441B9805 + 5318920
        at 28  UIKitCore                           0x00000001d496fd88 2A885557-9984-344A-9C09-B35AA177A4A6 + 13897096
        at 29  QuartzCore                          0x00000001b466faac 02C8CE74-F2CF-36F1-A9A7-6D4AD4BB4F43 + 1342124

Is there a workaround for this issue or is SQLDelight just not possible on tvOS?

@Joe3112
Copy link

Joe3112 commented Apr 30, 2020

You could use your tvOS app's cache directory to create and store the database file.
The problem is that it can get purged when your app is not running.
This is what i am doing:

val configuration = DatabaseConfiguration(
                name = "database",
                version = Database.Schema.version,
                foreignKeyConstraints = true,
                basePath = tvosDirPath("databases"),
                create = { connection ->
                    wrapConnection(connection) { LiveTvDb.Schema.create(it) }
                },
                upgrade = { connection, oldVersion, newVersion ->
                    wrapConnection(connection) { LiveTvDb.Schema.migrate(it, oldVersion, newVersion) }
                }
        )

        NativeSqliteDriver(configuration)

fun tvosDirPath(folder: String): String {
    val paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true);
    val documentsDirectory = paths[0] as String;

    val databaseDirectory = "$documentsDirectory/$folder"

    val fileManager = NSFileManager.defaultManager()

    if (!fileManager.fileExistsAtPath(databaseDirectory))
        fileManager.createDirectoryAtPath(databaseDirectory, true, null, null); //Create folder

    return databaseDirectory
}

@AlecKazakova
Copy link
Collaborator

looks like the above workaround is the only real action here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants