iPhone CoreData properties: changes to managedObjects are too slow

若如初见. 提交于 2020-01-03 05:11:23

问题


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 :-\


回答1:


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



来源:https://stackoverflow.com/questions/3595695/iphone-coredata-properties-changes-to-managedobjects-are-too-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!