tvOS: Temporary storage?

Ok, no permanent storage -- I get it. But is there a place I can use for temporary storage? Like, if I want to download a save-game state before parsing it into memory. Or if I want to write the game-state to a file, so I can apply zip and upload it.


Yes, I could probably do all of this in memory, but my current infrastructure is set up to use temporary files, and I'd love to just continue to use that, while only changing the tmpDir variable -- that'd be super sweet.


Is there any temporary directory to which I can write?


Thanks!

Replies

You can write data to the Caches directory and the data should stay there as long as your app is running. But when your app exits, that data could be deleted by the system, so be prepared for it to be gone when your app launches again.

The temporary directory is also availalble, but just as Justin described, it can be purged when your app is no longer running. But for the use case you describe, it should work just fine.

So to access Temporary folder I have to do


NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSSstring stringWithFormat:@"%d", appBundleId];


and the Cache folder like


NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSString *cacheFile = [cachesPath stringByAppendingPathComponent:@"file.plist"];


but there is no way to access to the Documents folder like


NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];


So, supposed the that app or a embedded framework needs to create da Sqlite database, there's no way to make it persistent at this time.

Are you sure? When I have tried to save to a file to cache directory I don´t get any error, but the file is not saved.

Yes pretty sure. I have downloaded the xcappdata from the AppleTV and I can see the sqlite database inside.
I'm working with fallbacks like




NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];

NSError *dbError=nil;

NSString *dbFolder = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"db.%@",APP_DATASTORE_VERSION]];

if ( ![[NSFileManager defaultManager] fileExistsAtPath:dbFolder] ) { // this works in iOS / watchOS

[[NSFileManager defaultManager] createDirectoryAtPath:dbFolder withIntermediateDirectories:YES attributes:nil error:&dbError]; /

}

if(dbError!=nil) { // this works in tvOS

dbError=nil;

NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

dbFolder = [cachesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"db.%@",APP_DATASTORE_VERSION]];

[[NSFileManager defaultManager] createDirectoryAtPath:dbFolder withIntermediateDirectories:YES attributes:nil error:&dbError]; /

}

There's an inconsistency regarding the Documents folder: you can create files in it when you run your app in the Simulator. But on the actual Apple TV, it just fails.

  • I am experiencing this also - when I use the .cachesDirectory, e.g. FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask), under TVOS, it works fine on the simulator but fails to find the file on physical Apple TVs. (using swiftUI)

Add a Comment