【问题标题】:Cannot save managed object with all attributes set无法保存设置了所有属性的托管对象
【发布时间】:2014-03-07 22:54:22
【问题描述】:

我正在尝试进入 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 和 ListenWish,而 Wish 是 ListenWish 的父实体。

我还为这两个设置了NSManagedObject 子类。

现在,在我的视图控制器中,我尝试使用如下文本字段保存一个 ListenWish 类型的新托管对象

DDAddWishViewController.h:

@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

DDAddWishViewController.m

...

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 行(分配推荐的字符串)。
换句话说:如果我只保留这些行中的一个一切正常(除了保存的数据不包含相应的字符串,当然),但如果我想同时保留它们,在尝试保存到 managedObjectContext 时会出现错误Could not save to data store (null), (null)

还有: 当我使用 Xcode 中的核心数据模型编辑器删除属性“recommendedBy” 并删除 Wish.h、Wish.m 以及当然在我的视图控制器中对它的每个引用时,删除应用程序并再次运行它我得到同样的错误Could not save to data store (null), (null)

我的模型有什么问题?我究竟做错了什么? :/

【问题讨论】:

    标签: ios objective-c xcode core-data xcode5


    【解决方案1】:

    你的支票

    if ( [self.wish.managedObjectContext save:&error]) {
    

    错了,应该是

    if (! [self.wish.managedObjectContext save:&error]) {
    

    因为save方法如果保存成功则返回TRUE,如果保存成功则返回NO 一个错误。如果您注释掉您的 A 或 B 行之一,那么保存 失败 因为 “title”和“recommendedBy”属性未标记为可选。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 2012-07-31
      • 2019-05-28
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多