重新启动应用程序后找不到保存的文件(在设备中) [英] Can't find saved file (in device) after restarting the app

查看:185
本文介绍了重新启动应用程序后找不到保存的文件(在设备中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以从图片中生成.pdf文件。如果我在拍摄照片的同一会话中读取文件,则文件会正确显示。

I have an app that generates a .pdf file from a picture. If I go read the file in the same session as I took the picture, the file shows up properly.

但是,如果我重新启动应用程序,则文件无法打开。当应用程序重新启动时,应用程序代码显然没有任何变化,因此位置引用保持不变。

However, if I restart the application, the file does not open. Obviously nothing changes in the code of the application when the app restarts, so the location references remain the same.

文件正保存到此位置:

/var/mobile/Containers/Data/Application/E119DC03-347B-4C84-B07B-C607D40D26B9/Documents/Test_1_Mod.pdf

奇怪的是,如果我转到Xcode的设备部分,我可以看到文件中的文件重新启动应用程序之前和之后的文件夹:

The odd part is that if I go to the "devices" section of Xcode, I can see the files in the Documents folder, before and after restarting the application:

编辑:以下是我如何获取保存文件的位置:

Here's how I'm getting the location to save the file:

NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
                                    NSDocumentDirectory,
                                    NSUserDomainMask,
                                    YES);
NSString *path = [arrayPaths objectAtIndex:0];
modified_pdfFileName = [path stringByAppendingPathComponent:modified_filename_pdf];

那么,我将文件保存在错误的位置吗?

So, am I saving the file in the wrong location?

文件是否在重启期间以某种方式移动?

Does the file move, somehow during the restart?

对此问题的任何建议?

谢谢

推荐答案

您不应存储持久性的原始文件路径(或者,如果您这样做,请知道根可以移动在你身上)。更好的做法是仅存储路径的相对部分并始终将其附加到当前的根路径(特别是如果您可能与iCloud一样跨设备共享数据)。

You shouldn't store raw file paths for persistence (or if you do, know that the root can move on you). A better practice would be to only store the relative part of the path and always attach it to the current "root" path in question (particularly if you might be sharing data across devices as with iCloud).

但是,通常采用快捷方式,所以这里有一些快速而肮脏的代码来获取文件路径(假设在Documents目录中,如果你正在使用它,你可以调整一下味道一个不同的位置)并将其更新到当前的Documents路径。

Still, it is common to take the shortcut, so here's some quick-and-dirty code to take a file path (assumed to be in the Documents directory, you can tweak to taste if you're using a different location) and update it to the current Documents path.

+ (NSString *)rebasePathToCurrentDocumentPath:(NSString *)currentFilePath 
{
    NSString *fileComponent = [currentFilePath lastPathComponent];
    NSArray *currentDocumentDir = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *currentDocumentPath = [currentDocumentDir objectAtIndex: 0];
    NSString *rebasedFilePath = [currentDocumentPath stringByAppendingPathComponent:fileComponent];
    return rebasedFilePath;
}

将此附加到utils类或以其他方式将其集成到您的流程中..

Attach this to a utils class or otherwise integrate it into your flow..

这篇关于重新启动应用程序后找不到保存的文件(在设备中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆