代码之家  ›  专栏  ›  技术社区  ›  Kevin Sylvestre

核心数据关系无法加载到一个关系

  •  2
  • Kevin Sylvestre  · 技术社区  · 15 年前

    我在文档和设置模型之间有一对一的关系:

    alt text

    创建文档时,会创建相应的设置对象。模型被持久化为核心数据。

    创建一些文档(和关联设置)后重新启动时,应用程序将使用以下方法重新加载要选择的文档列表:

    // Load delegate from shared application and context from delegate.
    SampleAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = delegate.managedObjectContext;
    
    // Create new request.
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
    // Create entity description using delegate object context.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:context];
    
    // Set entity for request.
    [request setEntity:entity];
    
    // Load array of documents.
    NSError *error;
    NSArray *documents = [context executeFetchRequest:request error:&error];
    
    // Rlease descriptor.
    [descriptor release];
    
    // Release request.
    [request release];
    
    // Done.
    return documents;
    

    列表看起来很好(文档正在加载)。但是,当试图访问任何文档的设置时,程序退出时给出:

    当用户选择按钮时,会触发对设置的修改。奇怪的是,如果在加载时返回文档之前添加了以下代码段(即,如果在加载时访问了关系),则程序不会崩溃:

    for (Document *document in documents)
    {
        NSLog(@"document.name: %@", document.name);
        NSLog(@"document.settings.stroke: %@", document.settings.stroke);
        NSLog(@"document.settings.stroke: %@", document.settings.stroke);
    }
    

    关系的属性是“(nonatomic,retain)”并使用“@dynamic”。有什么想法吗?谢谢!

    2 回复  |  直到 15 年前
        1
  •  0
  •   Graham Perks    15 年前

    在核心数据模型中,是否设置了文档和设置之间的反向关系?如果不是,你必须。

        2
  •  0
  •   Kevin Sylvestre    14 年前

    在这个错误上花了太多时间之后,我终于明白了。在值的初始加载期间,我设置了一个KVO。此代码导致崩溃崩溃(尽管直到下一个运行循环才出现崩溃):

    [self.document.settings addObserver:self forKeyPath:@"fill" options:0 context:NULL];
    [self.document.settings addObserver:self forKeyPath:@"stroke" options:0 context:NULL];
    

    推荐文章