核心数据:setPrimitiveValue 并保存更改
在核心数据中,当使用 setPrimitiveValue 与对象的常规访问器方法进行更改时,是否有一些技巧可以保存对托管对象属性的更改?
我已在某些情况下改用 setPrimitiveValue 和 setPrimitiveAttributeName 以避免触发 FRC 的通知方法(controllerWillChangeContent、controllerDidChangeObject 等)。在这两种情况下,在对核心数据对象进行更改并保存托管对象上下文之后,更改看起来像是已进行(即我可以使用 NSLog 看到更改),但它们实际上丢失了(即如果我在模拟器并重新运行它,FRC 重新加载,我认为我更改的属性消失了)。
保存代码是从 Apple 示例复制的,而我的 Core Data 实体的托管对象类是由 Xcode 生成的。我从 Apple 和本网站上读到的所有内容都表明这是 setPrimitive 方法的有效使用,尽管我承认我对这些 setPrimitive 函数的实际工作原理缺乏一定的了解...
代码:
// header file for Managed Object sub class generated by xcode from my core data entity:
#import <CoreData/CoreData.h>
@interface PlannedItem : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * ptType;
@property (nonatomic, retain) NSNumber * whenOrder;
@end
// .m file for Managed Object subclass
#import "PlannedItem.h"
@implementation PlannedItem
@dynamic ptType;
@dynamic whenOrder;
@end
// trying to use setPrimitiveValue or even setPrimitiveWhenOrder to modify
// this is in my view controller
// p is a pointer to PlannedItem
int oldOrder = [p.whenOrder intValue];
[p setPrimitiveValue:[NSNumber numberWithInt:(oldOrder +1)] forKey:@"whenOrder"];
// later on in same method, save is executed.
if (![[self managedObjectContext] save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSLog(@"Saved");
感谢您的任何提示
In Core Data, is there some trick to saving changes to managed objects' attributes when the changes were made with setPrimitiveValue versus the object's regular accessor methods?
I've switched to using setPrimitiveValue and setPrimitiveAttributeName in a few scenarios in order to avoid triggering my FRC's notification methods (controllerWillChangeContent, controllerDidChangeObject, etc.). In both cases, after making changes to the core data objects and saving the managed object context, the changes look like they were made (i.e I can see the changes with NSLog) but they are actually lost (i.e. if I exit the application in the simulator and rerun it, the FRC reloads, and the attributes I thought I changed are gone).
The save code is copied from Apple examples, and the managed object classes for my Core Data entities are as-generated by Xcode. Everything I've read from Apple and on this site suggests this is a valid use of the setPrimitive methods, though I admit I must be lacking a some understanding of how these setPrimitive functions really work...
Code:
// header file for Managed Object sub class generated by xcode from my core data entity:
#import <CoreData/CoreData.h>
@interface PlannedItem : NSManagedObject
{
}
@property (nonatomic, retain) NSNumber * ptType;
@property (nonatomic, retain) NSNumber * whenOrder;
@end
// .m file for Managed Object subclass
#import "PlannedItem.h"
@implementation PlannedItem
@dynamic ptType;
@dynamic whenOrder;
@end
// trying to use setPrimitiveValue or even setPrimitiveWhenOrder to modify
// this is in my view controller
// p is a pointer to PlannedItem
int oldOrder = [p.whenOrder intValue];
[p setPrimitiveValue:[NSNumber numberWithInt:(oldOrder +1)] forKey:@"whenOrder"];
// later on in same method, save is executed.
if (![[self managedObjectContext] save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSLog(@"Saved");
Thanks for any tips
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不希望执行通知处理程序中的代码(顺便说一句,您为什么要这样做?),禁用该代码可能会更容易,而不是完全避免通知。另外,请记住,当您进行更改时,核心数据会使用这些通知来更新您的关系并保持模型的一致性。
在您的视图控制器中,设置一个布尔值并根据您的需要设置它是否执行通知处理程序代码。然后,在您的处理程序中检查该标志,例如:
If you don't want the code in the notification handlers to execute (BTW, why would you want that?), it might be easier to disable that, instead of completely avoiding the notifications. Also, remember that Core Data uses those notifications to update your relationships and maintain coherence in your model when you make changes.
In your view controller, setup a boolean and set it according to your need to execute the notification handler code or not. Then, in your handlers check that flag, e.g.: