iPhone CoreData 属性:对 ManagedObjects 的更改太慢
我的 iPhone 应用程序中有一个 CoreData 模型,它链接到一个包含超过 50k 条记录的 SQL 数据库。当我生成记录类时,Xcode 使用 @dynamic 指令作为属性。我有一个名为“ISFAV”的属性,NSNumber 类型(CoreData 不使用 BOOL 或 Integer,它使用对象类型)。简而言之,当用户以这种方式点击按钮时,我会更改 ISFAV 属性:
if (![record.ISFAV intValue])
record.ISFAV=[NSNumber numberWithInt:1];
别的 record.ISFAV=[NSNumber numberWithInt:0];
很简单。但如果我尝试连续多次点击同一个按钮,iPhone 会花费太多时间(按钮保持保持状态的时间逐渐增加)。即使我更改记录,按顺序从收藏夹中添加/删除许多记录(而不是从收藏夹中添加/删除相同记录)也会发生这种情况。
如果我将原来的访问器方法更改为@synthesize,问题似乎就解决了。
在 CoreData 中对访问器方法使用合成指令是否正确?
非常感谢!
@编辑 使用合成指令,当我保存上下文时,不会对 CoreData 模型进行任何更改:-\ 问题仍未解决:-\
I have a CoreData model in my iPhone app, which is linked to a SQL Database with more than 50k records. When I generate the records classes, Xcode uses the @dynamic directive for properties. I have a property named "ISFAV", NSNumber type (CoreData does not use BOOL or Integer, it uses object types). Being short, I change the ISFAV property when the user taps on a button in this way:
if (![record.ISFAV intValue])
record.ISFAV=[NSNumber numberWithInt:1];
else
record.ISFAV=[NSNumber numberWithInt:0];
Quite simple. But if I try to tap many times on the same button sequentially, the iPhone takes too much time (the button remains in the hold state for a time that increase progressively). This happens even if I change record, adding\removing many records from favorites sequentially (instead of adding\deleting the same record from favorites).
If I change the original accessor method to @synthesize, the problem seems to be solved.
Is it correct to use the synthesize directive for accessor methods in CoreData?
Thank you very much!
@edit
Using the synthesize directive, no changes are made to the CoreData model when I save the context :-\ The problem is still unsolved :-\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@dynamic
是一个标志,它只是告诉编译器该方法将在运行时存在,并且现在不发出警告。您不应该将@synthesize
与 Core Data 属性一起使用。通过设置 Core Data 属性,您如何知道您的热点?你分析过代码吗?根据我的经验,更改 Core Data 中的一个属性不会很慢,而是 1/1000 秒或更快。每次更改该属性时是否都会保存到磁盘?您在通话中还有其他事情吗?
我会首先分析代码并找出热点的真正位置。使用仪器并确认。
@dynamic
is a flag that just tells the compiler that the method will exist at run time and to not warn about it now. You should not be using@synthesize
with Core Data properties.How do you know your hotspot is with setting the Core Data property? Have you profiled the code? In my experience, changing one attribute in Core Data is not going to be slow, it will be 1/1000th of a second or faster. Are you saving to disk each time you change that one property? Are you doing something else in the call?
I would profile the code first and find out where the hotspot really is. Use Instruments and confirm.