为什么我的核心数据模型不再起作用? Integer16 值翻转?
因此,我将一个 iPad 应用发送到去年三月的应用商店。它包含一个核心数据模型,其中包含预先填充的二叉树数据存储。我编写了一个小型 64 位 OSX 应用程序,用于将任意 200MB 的 SQLite 数据库中的数据转换并提取到我的自定义核心数据模型,从而形成一个整洁的 20MB 大小的 SQLite 持久存储。所有的花花公子,投入版本控制,再也没有回头。
现在我的客户有了更新的数据库,他们也想发布更新的应用程序。我认为这只需再次运行转换器并重新发布到 iTunesConnect,但事实并非如此。我的转换器无法运行,或者无法输出所需的存储。我花了几个小时试图找出问题所在,最后将所有内容恢复到我最初向商店提交应用程序时的状态,猜猜看,转换器甚至不会转换它。相同的代码,相同的输入数据库,相同的一切!
当查看我的应用程序生成的一些数据时,我发现某些值在核心数据模型中的存储方式存在奇怪的差异。在这三行中,组对象是一个核心数据实体。标识符属性输入为Integer 16
,模型中所有基于整数的值也是如此。有趣的是,当查看这三行之后的值时:
int identifier = 39899;
NSNumber *numIdentifier = [NSNumber numberWithInt:identifier];
group.identifier = numIdentifier;
我得到这三个值:
identifier: 39899
numIdentifier: 39899
group.identifier: -25637
呃,什么?该数字当然源自源数据库,但即使手动插入它,核心数据实体上的最后一个属性也会出现乱码。到底为什么最后一行不一样?负什么?当然,这个值甚至没有接近 INT_MAX,为什么它看起来像是包装了一个有符号 int ?为什么现在与三月份时的情况有所不同,使用相同的代码和相同的输入数据库它以前工作得很好?从那时起我记得唯一改变的是 OSX Lion 的升级。但这肯定不会影响这一点,对吗?
有人知道我做错了什么,去年三月我可能已经做错了什么,以及我该如何解决这个烂摊子?
So I shipped an iPad app to the app store last march. It contains a Core Data model with a prepopulated store of binary tree data. I had written a tiny 64-bit OSX app to convert and extract data from an arbitrary 200MB weighing SQLite database to my custom Core Data model, resulting in a neat 20MB sized SQLite persistent store. All dandy, put into version control and never looked back.
Now my client has an updated database and they want to ship an updated app too. I figured this wouldn't take more than running the converter again and re-publishing to iTunesConnect, but no. My converter wouldn't run, or it wouldn't output the desired store. I spent hours and hours trying to figure out what was wrong, before finally reverting everything back to when I submitted my app to the store originally, and guess what, the converter wouldn't even convert that. Same code, same input database, same everything!
When looking at some data that my app resulted with, I found a weird discrepancy in how some values get stored in the Core Data model. In these three lines the group object is a Core Data entity. The identifier property is typed to Integer 16
, as are all my integer based values in the model. Funny enough, when looking at the values after these three lines:
int identifier = 39899;
NSNumber *numIdentifier = [NSNumber numberWithInt:identifier];
group.identifier = numIdentifier;
I get these three values:
identifier: 39899
numIdentifier: 39899
group.identifier: -25637
Err, what? The number would of course stem from the source database, but even when inserting it manually, the last property on the core data entity gets garbled. Why on earth is that last line different? Negative what? Surely the value doesn't even come close to INT_MAX, why does it look like it's wrapping a signed int? And why is it different now when back in march, with the same code and same input database it used to work just fine? The only thing I can remember changing since then is an upgrade to OSX Lion. But surely that couldn't have affected this, right?
Would someone know what I'm doing wrong, what I've maybe been doing wrong last march already, and how I can fix this mess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正常的
int
是 32 位,而不是 16 位。虽然 32 位对于该值来说足够大,但 16 位却不够! -25637 是溢出值。您要么需要更小的数字,要么需要更大的变量。A normal
int
is 32 bits, not 16. While 32 bits is plenty large enough for that value, 16 is not! -25637 is the overflowed value. You either need smaller numbers, or bigger variables.参见
“iOS 5 中的 CoreData 和整数宽度”
http://www .seattle-ipa.org/2011/09/11/coredata-and-integer-width-in-ios-5/
“在 iOS 3 或 4 中,您可以存储更宽的整数比你的模型指定的要多,但在 iOS5 中,整数的宽度现在被强制执行。”
相关堆栈溢出问题
核心数据将属性从整数 16 更改为整数 32
See also
"CoreData and Integer Width in iOS 5"
http://www.seattle-ipa.org/2011/09/11/coredata-and-integer-width-in-ios-5/
"In iOS 3 or 4 you could get away with storing a wider integer than your model specifies but in iOS5 the width of integers is now being enforced."
Related Stack Overflow question
Core Data change property from Integer 16 to Integer 32