我正在尝试进入Core Data。我还阅读了大部分与Core Data相关的Apple Developer文档,但我有一个奇怪的问题:
我使用Core Data创建了一个名为“Wishes”的Xcode项目,因此大部分内容都是自动设置的。我像这样设置了我的xcdatamodel:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="5059" systemVersion="13B42" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="ListenWish" representedClassName="ListenWish" parentEntity="Wish" syncable="YES">
<attribute name="album" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="artist" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="year" optional="YES" attributeType="Integer 16" defaultValueString="0" syncable="YES"/>
</entity>
<entity name="Wish" representedClassName="Wish" isAbstract="YES" syncable="YES">
<attribute name="dateAdded" attributeType="Date" syncable="YES"/>
<attribute name="dateModified" attributeType="Date" syncable="YES"/>
<attribute name="rating" attributeType="Integer 16" minValueString="0" maxValueString="6" defaultValueString="0" syncable="YES"/>
<attribute name="recommendedBy" attributeType="String" indexed="YES" syncable="YES"/>
<attribute name="title" attributeType="String" maxValueString="64" indexed="YES" spotlightIndexingEnabled="YES" syncable="YES"/>
</entity>
<fetchRequest name="BiggestWishes" entity="Wish" predicateString="rating == 6"/>
<elements>
<element name="ListenWish" positionX="-63" positionY="-0" width="128" height="88"/>
<element name="Wish" positionX="79" positionY="-190" width="128" height="120"/>
</elements>
</model>
所以基本上有两个实体描述:Wish&amp; ListenWish ,而Wish是ListenWish的父母权利。
我还为这两个设置了NSManagedObject
个子类。
现在在我的视图控制器中,我正在尝试使用如下文本字段保存一个类型为ListenWish的新托管对象:
@interface DDAddWishViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *artistField;
@property (weak, nonatomic) IBOutlet UITextField *albumField;
@property (weak, nonatomic) IBOutlet UITextField *yearField;
@property (weak, nonatomic) IBOutlet UISlider *ratingSlider;
@property (weak, nonatomic) IBOutlet UITextField *recommendedByField;
@property (strong, nonatomic) ListenWish *wish;
- (IBAction)saveWish:(id)sender;
- (IBAction)cancel:(id)sender;
@end
...
DDAppDelegate *appDelegate = (DDAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
// Create a new managed object of type ListenWish
self.wish = [NSEntityDescription insertNewObjectForEntityForName:@"ListenWish" inManagedObjectContext:context];
self.wish.artist = self.artistField.text;
self.wish.album = self.albumField.text;
self.wish.year = [NSNumber numberWithInteger:[self.yearField.text integerValue]];
self.wish.title = [NSString stringWithFormat:@"%@ - %@", self.wish.artist, self.wish.album]; // line A.
self.wish.rating = [NSNumber numberWithFloat:self.ratingSlider.value];
self.wish.recommendedBy = self.recommendedByField.text; // line B.
NSError *error = nil;
if ( [self.wish.managedObjectContext save:&error]) {
//TODO: Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Could not save to data store %@, %@", [error localizedDescription], [error userInfo]);
abort();
}
现在奇怪的是:所有都会按预期工作,但仅当我注释掉A行(指定标题字符串)或B行(指定recommendedBy字符串)时。
/>
换句话说:如果我只保留一行 ,一切正常(除了保存的数据不包含相应的字符串,当然),但如果我想保留它们两者,我在尝试保存到managedObjectContext时出现错误:Could not save to data store (null), (null)
此外:
当我使用Xcode中的核心数据模型编辑器删除属性“recommendedBy”并删除Wish.h,Wish.m中的每个引用,当然还有我的视图控制器中删除应用并再次运行我收到相同的错误:Could not save to data store (null), (null)
我的模特有什么问题?我究竟做错了什么? :/
答案 0 :(得分:1)
您的支票
if ( [self.wish.managedObjectContext save:&error]) {
错了,应该是
if (! [self.wish.managedObjectContext save:&error]) {
因为如果保存成功,save
方法会返回TRUE
,而如果保存成功则返回NO
一个错误。如果您注释掉A或B中的一行,则保存将失败,因为
“title”和“recommendedBy”属性未标记为可选。