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

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

问题描述

我的第一个应用更新昨晚刚刚上线,我收到了一个投诉,说更新导致用户创建的数据(其中一些)消失了.我已经能够重现该问题,但不知道为什么.

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.

在 Documents 目录中,我保存了一个密钥文件,它告诉我所有用户文件的标题"及其文件名(完整路径).那么所有用户的文件也都在Documents目录下.

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.

这是我的第一个应用程序和我的第一次更新,我在这里很茫然.我暂时停止了该应用程序的销售(不知道您不能简单地恢复到旧版本!哎呀!)并且非常感谢有关在哪里/如何查找问题以及如何编写的任何想法不会丢失数据的好更新.(到目前为止,我所发现的只是将数据保存在 Documents 目录中",这就是我已经在做的.)

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.)

我确实在自己的项目中保存了原始应用程序,我可以返回并重新开始工作.当我开始进行更新时,我复制了整个目录,我想知道这是否可能是问题所在?我确实更改了包含所有 XCode 文件的目录的名称,并使用了 Project>Rename 函数.这会以某种方式产生这种效果吗?

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?

应用(原始版和更新版)在 4.2 及更高版本上运行(如果有的话).

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

解决方案:

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

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).

因此,用户文件已被传输到更新的新 Documents 目录中,但我一直在旧的绝对路径中寻找它们,即不在我的沙箱中.

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.

通过在应用程序的 didFinishLaunching 中放置一个循环来提取有问题的文件路径文件(那个,我一直在本地寻找并且它仍在工作)并将它们分解为文件名.每当执行文件操作时,都必须以编程方式找到并添加文档路径.

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.

推荐答案

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

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/

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

在那里您会看到名称如下的文件夹:

There you'll see folders with names like:

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

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

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

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

文件图书馆时间YourApp.app

Documents Library tmp YourApp.app

通过引用整个 URL,您会看到类似的内容,

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

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

但是,当您更新应用程序时,该应用程序将尝试的新 URL 将是:

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

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

这是空的.

相反,您应该始终通过仅在获得文档目录文件路径前缀后保存文件路径来进行动态引用.

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];
}

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

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天全站免登陆