第一次应用程序更新,用户数据丢失(存储在Documents目录中) [英] First App Update, User Data Lost (was stored in Documents directory)

查看:410
本文介绍了第一次应用程序更新,用户数据丢失(存储在Documents目录中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个应用程序更新昨天刚刚上线,我收到了一个抱怨:​​更新导致用户创建的数据(其中一些)消失。我已经能够重现这个问题,但不能说明为什么。

在文档目录中,我保存了一个关键文件,告诉我标题的所有用户的文件和文件名(完整路径)。然后所有用户的文件也在Documents目录中。

当更新发生时,密钥文件仍然存在(至少,我认为这是因为数据显示在应用程序的第一个屏幕上显示 - 应用程序完全退出,并在更新后重新启动,对吗?),但是当用户试图导航到实际的文件,没有数据和用户输入的任何新数据永远不会被保存。

它的行为与我在调试时的意外情况是一样的,当时我意外地使用了一个无效的文件名(不正确的字符) - 它从来没有保存过。但随着更新,这些文件已被正确保存在旧版本,但在新版本中以某种方式失败。



这是我的第一个应用程序,我的第一次更新和我在这里相当茫然。我已经从现在的销售中拉出了应用程序(不知道你不能简单地回到旧版本!),并且深深地感谢关于在哪里/如何寻找问题以及如何写一个好的更新,不会丢失数据。 (到目前为止,我发现的所有内容都是将数据保存到文档目录中,这正是我已经在做的。)



我确实保存了原始应用程序在自己的项目中,我可以再回去工作。当我开始进行更新时,我复制了整个目录,而且我怀疑这是否会成为问题?我改变了保存所有XCode文件的目录名称,并使用了Project> Rename函数。莫名其妙地有这个效果吗?



应用程序(包括原始版本和更新版本)在4.2及更高版本上运行,如果有问题的话。

解决方案:

我相信我已经找到了这个问题。就像我在问题开始时所说的那样,我将用户文件的完整路径保存在我的密钥文件中。显然,完整的路径不能保证在更新后是相同的(我确定这是记录在某处,但我没有跑过它)。

所以,用户文件HAD被传送到更新的新文档目录,但是我正在以旧绝对路径(即不在我的沙箱中)查找它们。

已修复在应用程序的didFinishLaunching中放置一个循环来取出文件路径的问题文件(我一直在本地查找,并且仍然在运行),然后将它们截断到文件路径。只要执行文件操作,就必须找到并添加文档路径。



对于它的价值,实际上对于我来说,没有办法恢复到因为重新安装原始版本,商店中的以前的二进制文件不会固定用户的问题,但我会相信它,至少在最初。我希望有一种方法可以禁止更新,但仍然允许新购买(因为问题不会影响新的购买)。可能不是一个足够普遍的情况来保证它。

解决方案

只是为了说明这一点,为互联网路过。是的,OP最后回答了他自己的问题。您不应该在文档目录中存储文件的绝对URL,否则可能会导致数据丢失 。这是因为在更新应用程序时,通过更改.plist文件中的版本号,iOS会为该应用程序使用不同的十六进制名称创建一个新目录。现在,您的绝对网址引用了错误的位置,并且不会返回正确的文件。



在您通过导航部署到设备之前,您可以在自己的计算机上看到

〜/ Library / Application Support / iPhone Simulator / * ios_version * / Applications /



会看到名称类似的文件夹:

6AA4B05C-8A38-4469-B7BE-5EA7E9712510
CB43C5F3-D720-49C3-87D4-EBE93FFD428B



这些文件夹内将是iOS应用程序的标准文件系统布局。即

文件
图书馆
tmp
YourApp.app


引用整个URL,你会看到类似的,

/ Library / Application Support / iPhone Simulator / * ios_version * / Applications / 6AA4B05C-8A38-4469-B7BE然而,当你更新应用程序时,应用程序将尝试的新URL将是:




$ b

$ b

〜/ Library / Application Support / iPhone Simulator / * ios_version * / Applications / CB43C5F3-D720-49C3-87D4-EBE93FFD428B / Documents / MyVeryImportantUserData.txt

是空的。



相反,您应该总是在获取文档目录文件路径前缀后保存文件路径来创建动态引用。

  / ** 
返回应用程序的Documents目录的路径。
* /
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];

$ / code>

在原始海报中,您可以保存您的用户通过发布新的更新来丢失数据,在该更新中您修改了代码以获取绝对URL并通过字符串的文档部分修剪所有内容。然后,您可以在文件路径前加上正确的文档目录url,应用程序就可以找到数据。


My first app update just went live last night and I've gotten a complaint that the update caused the user-created data (some of it) to disappear. I have been able to reproduce the problem, but can't tell why.

In the Documents directory, I have saved one key file which tells me the "title" of all the user's files and their filenames (full path). Then all the user's files are also in the Documents directory.

When the update occurs, the key file is still there (at least, I think it is because the data shows up in the first screen of the app - apps do fully quit and relaunch after updating, right?), but when the user tries to navigate into the actual files, there is no data and any new data the user types in is never saved.

It's acting exactly as it did in debugging when I was accidentally using an invalid filename (with improper characters in it) - it never saved. But with the update, these files had been saved properly in the old version but are somehow failing in the new version.

This is my first app and my first update and I'm quite at a loss here. I've pulled the app from sale for the moment (didn't know you couldn't simply revert to an old version! yikes!) and would deeply appreciate any ideas as to where/how to look for the problem and how to write a GOOD update that won't lose the data. (So far, all I've found is "save data in the Documents directory", which is what I was already doing.)

I do have the original app saved in its own project and I can go back and work from that again. I copied that whole directory when I started working on the update and I do wonder if that could somehow be the problem? I did change the name of the directory that holds all the XCode files and used the Project>Rename function. Could that have this effect somehow?

The app (both original and update) is running on 4.2 and above, if that matters.

RESOLUTION:

I believe I have figured out this problem. Like I said near the beginning of my question, I was saving the FULL PATH of the user files in my key files. Apparently, the full path is NOT guaranteed to be the same after an update (I'm sure this is documented somewhere, but I hadn't run across it).

So, the user files HAD been transported to the update's new Documents directory, but I was looking for them at the old absolute path, i.e. not in my sandbox.

Fixed by putting a loop in app's didFinishLaunching to pull out the offending file of filepaths (THAT, I had always looked for locally and it was still working) and chop them down to just fileNAMES. The Documents path has to be found and added programmatically whenever file operations are performed.

For what it's worth, it was actually good for me that there is no way to revert to a previous binary in the store because re-installing the original version would not have fixed the user's problem, but I would have believed it had, at least initially. I do wish there was a way to have disallowed UPDATES, but still allowed new purchases (since the problem did not affect new purchases). Probably not a common enough situation to warrant it, though.

解决方案

Just to shed some more light on this for internet passer-bys. Yes, the OP answered his own question at the very end. You should never store absolute URLs for files in your documents directory and doing so may cause data loss. This is because when you update an app, by changing its version # in the .plist file, iOS creates a new directory for that app with a different hexadecimal name. Now your absolute URL is referencing the incorrect location and won't return the proper file.

You can see this on your own computer before you even deploy to a device by navigating to

~/Library/Application Support/iPhone Simulator/*ios_version*/Applications/

There you'll see folders with names like:

6AA4B05C-8A38-4469-B7BE-5EA7E9712510 CB43C5F3-D720-49C3-87D4-EBE93FFD428B

Inside those folders will be the standard file system layout for iOS apps. i.e.

Documents Library tmp YourApp.app

By referencing the entire URL, you'll see something like,

~/Library/Application Support/iPhone Simulator/*ios_version*/Applications/6AA4B05C-8A38-4469-B7BE-5EA7E9712510/Documents/MyVeryImportantUserData.txt

However when you update the app the new URL that the app will try will be:

~/Library/Application Support/iPhone Simulator/*ios_version*/Applications/CB43C5F3-D720-49C3-87D4-EBE93FFD428B/Documents/MyVeryImportantUserData.txt

which is empty.

Instead you should always make a dynamic reference by only saving the filepath after you've gotten the documents directory filepath prefix.

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

To the original poster, you could have saved your users lost data by issuing a new update where you modified your code to take the absolute url and trim everything through the documents portion of the string. Then you could prefix the filepath with the proper documents directory url and the app would have found the data.

这篇关于第一次应用程序更新,用户数据丢失(存储在Documents目录中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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