Skip to content

Conversation

JPKribs
Copy link
Member

@JPKribs JPKribs commented Jul 20, 2025

Summary

Resolves: #776
Discussed: #1654 (comment)

From some feedback, I believe this resolves our tvOS logout issue by moving our data from .cachesDirectory to .applicationSupportDirectory. This should mirror where this is places for iOS now. I have some build flags but I believe this should also work by using the same code for both iOS and tvOS. I wanted to start at this point to keep it separate since iOS is more up-to-date and I wanted to limit changes to the polished side of Swiftfin.

I have added a migration as well to move files from the .cachesDirectory to .applicationSupportDirectory if available. I put this in Mappings but please let me know if this is the incorrect move.

This is WIP for now. I am going to throw some more testing at this first. I've done some testing this morning but I want to test a bit more to be safe. Additionally, I want to test the migration as well. Specifically, file migration + upgrade from 1.0.1 to Main.


Progress

  • Test & Confirm that the new SQLite location prevents data loss / logout issues
  • Migrate a 1.0.1 SQLite file to .applicationSupportDirectory

@JPKribs JPKribs added bug Something isn't working developer Improves or alters the developer experience labels Jul 20, 2025
@JPKribs JPKribs changed the title [WIP] Migrate tvOS from .cachesDirectory to .applicationSupportDirectory [WIP] Resolve: Logged Out Between Sessions [tvOS] Jul 20, 2025
@JPKribs JPKribs marked this pull request as ready for review July 20, 2025 05:53
@JPKribs
Copy link
Member Author

JPKribs commented Jul 20, 2025

@LePips I think this is good to go! I've successfully migrated 10 different simulators and confirmed they are working as expected. I adding a lot of logging for this but please let me know if it's done incorrectly or needs to be scaled up/down. Most of the logging I use in my day-to-day are for logs only I see so please let me know if there is a more standard way I should be approaching this.

I've been testing this by loading everything onto a physical AppleTV. I will have the AppStore 1.0.1 version logged in and this branch logged in. I will then let both of these go into the background / close Swiftfin. I use VLC's tvOS to upload videos into the AppleTV's storage until it is full. Once it's full, I check both Swiftfins and find that 1.0.1 have been 'logged out' where the data has been cleared with the cache and this branch is still active and logged in.

Please let me know if there is a more scientific method for testing this. I know this route is kind of a pain but I've been testing this one for the day and this appears to have fixed the "log out" issue. Either that, or it's just more resilient? Regardless, I think you were right that this is the correct move for resolving this!

Finally, In my testing, the logic I have for the the tvOS SQLiteStore should be identical to what iOS is doing with the CoreStore defaults. However, I wanted to minimize impact so I left iOS intact as-is. Please let me know if that isn't the right move. Below is the item I am referring to:

New SQLiteStore
    private static let storage: SQLiteStore = {

        let logger = Logging.Logger.swiftfin()

        let applicationSupportDirectory = FileManager.default.urls(
            for: .applicationSupportDirectory,
            in: .userDomainMask
        ).first!

        let databaseURL = applicationSupportDirectory.appendingPathComponent(bundleIdentifier, isDirectory: true)
            .appendingPathComponent("Swiftfin.sqlite")

        logger.info("Using the SQLite Database at: \(databaseURL.absoluteString)")

        #if os(tvOS)

        return SQLiteStore(
            fileURL: databaseURL,
            migrationMappingProviders: [Mappings.userV1_V2]
        )
        
        #else

        return SQLiteStore(
            fileName: "Swiftfin.sqlite",
            migrationMappingProviders: [Mappings.userV1_V2]
        )

        #endif
    }()

@JPKribs JPKribs changed the title [WIP] Resolve: Logged Out Between Sessions [tvOS] Resolve: Logged Out Between Sessions [tvOS] Jul 20, 2025
@JPKribs JPKribs requested a review from LePips July 20, 2025 06:28
@LePips
Copy link
Member

LePips commented Jul 20, 2025

Please test after my changes to see if it still works.

@JPKribs
Copy link
Member Author

JPKribs commented Jul 20, 2025

Please test after my changes to see if it still works.

I'll start testing now but I will likely need a couple hours get to run through it all. I'll reach out when I am done!

@JPKribs
Copy link
Member Author

JPKribs commented Jul 20, 2025

@LePips

I haven't done any testing for the logout portion of this. I realize that we're using the same locations so this should not require new testing. However, our migration was failing. It was strange, the SQLite file was being moved over but none of the logged in accounts were there. The issue was the sqlite-wal wasn't moving over:

Screenshot 2025-07-20 at 16 47 46

My understanding of the sqlite-wal is that this is just unprocessed transactions so this could just be that I am creating 1.0.1 then immediately attempting to migrate it, but IMO, this would make sense to move the the wal and shm over as well even if I don't believe the shm will impact anything. If anything, just to clean up after ourselves.

I added a commit that works in migrating these auxiliary files as well. I am only guarding on the SQLite since we only want to migrate the WAL and SHM if the SQLite moves so we're not mixing and matching auxiliary files.

I'm 100% sure you will have a better way of doing this than the [URL] haha. That being said, this latest commit works in resolving the migration portion of this. Please let me know if you need to me to test the logout portion again as well. I skipped this part since it's a slow test and we're using the same directories as prior but I'm not opposed if you feel it's warranted.

Copy link
Member

@LePips LePips left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for testing!

@LePips LePips merged commit 3c51138 into jellyfin:main Jul 20, 2025
4 checks passed
@JPKribs JPKribs deleted the tvOSCacheLocation branch July 20, 2025 23:32
@sjorge
Copy link

sjorge commented Jul 21, 2025

Building current main for tvOS seems to result in a broken Swiftfin atm, it just hangs at a spinner. I think it's due to this PR.

The dev console logs:
[🔴 Error] DataStack+Migration#244:addStorage(_:completion:) Failed to load SQLite 'NSPersistentStore' metadata.
[🔴 Error] SwiftfinStore#103:setupDataStack() Failed creating datastack with: The operation couldn’t be completed. (com.corestore.error error 5.)

When running on hardware (it previously had another Swiftfin tvOS installed build from main a week or so ago.

Running it in a simulator is fine though (but that one was empty)

Edit: removing swiftfin and rebooting the tvOS box results in the same and installing again results in the same.
Edit2: building 41d470e (the commit before this merge on main works fine)

@JPKribs
Copy link
Member Author

JPKribs commented Jul 21, 2025

@LePips I am so sorry, @sjorge is right... I swear I tested this in like 30 different ways but I must have missed something. Clean installs are screwing up on hardware.

I think is best to rollback while this is fresh. I can keep digging on my end to find a better route.

Sorry, I know that's a huge pain...

@sjorge
Copy link

sjorge commented Jul 21, 2025

Glad I at least caught pretty quickly :) I try to bump my copy to main once a week or so.

LePips added a commit to LePips/SwiftFin that referenced this pull request Jul 21, 2025
@LePips
Copy link
Member

LePips commented Jul 21, 2025

#1660

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working developer Improves or alters the developer experience
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logged Out Between Sessions [TVOS]
3 participants