我有一个控制器,它是工作流的根本。如果工作流没有数据对象,则创建一个新的对象,如果有,则使用现有的对象。我有一个模型对象的属性(一个NSManagedObject)

@property (nonatomic, retain) Round *currentRound;

每当显示相应视图时,我都会调用以下内容
self.currentRound = [self.service findActiveRound];
if (!self.currentRound) {
    NSLog((@"configure for new round"));
    self.currentRound = [self.service createNewRound];
    ...
} else {
    NSLog(@"configure for continue");
    // bad data here
}

问题出在上面标记的位置,有时数据对象已损坏。在我未显示的部分中,我在一些文本字段中设置了值以表示模型中的值。有时还可以,但最终模型对象上的属性为空并且发生故障

在调试器中,对该回合的引用似乎没有更改,但是NSLogging相关属性显示它们无效。调试似乎会延迟损坏的发生。

我知道我没有保存上下文...应该这样吗?如果是这样,为什么我第一次回到该控制器时不会总是失败?

我的findActiveRound消息没什么特别的,但是万一重要
-(Round *) findActiveRound
{
    NSLog(@"Create Active Round");
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Round" inManagedObjectContext:context];
    [request setEntity:entity];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"isComplete == %@", [NSNumber numberWithBool:NO]];
    [request setPredicate:pred];

    NSError *error = nil;
    NSArray *results = [context executeFetchRequest:request error:&error];

    if ([results count] == 0) {
        return nil;
    } else {
        return [results objectAtIndex:0];
    }
}

许多thanx。

编辑响应

损坏是指当我尝试从模型对象中获取一些简单的字符串属性时,会得到nil值。因此,在上面的代码中(我认为我有一个回合),我会做类似
self.roundName.text = self.currentRound.venue;
self.teeSelection.text = self.currentRound.tees;

而且看不到我输入的数据。由于它有时仅会失败,但最终总是会失败,因此我会看到输入的数据在消失之前会经过一段时间。

我很确定上下文是相同的。我的服务是单例的,创建起来像这样
@implementation HscService

+(id) getInstance
{
    static HscService *singleton = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        singleton = [[self alloc] init];
    });
    return singleton;
}

-(id) init
{
    if (self = [super init]) {
        model = [NSManagedObjectModel mergedModelFromBundles:nil];
        NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

        NSString *path = [self itemArchivePath];
        NSURL *storeUrl = [NSURL fileURLWithPath:path];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES],
                                 NSMigratePersistentStoresAutomaticallyOption,
                                 [NSNumber numberWithBool:YES],
                                 NSInferMappingModelAutomaticallyOption, nil];

        NSError *error = nil;
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
            [NSException raise:@"Open failed" format: @"Reason: %@", [error localizedDescription]];
        }

        context = [[NSManagedObjectContext alloc] init];
        [context setPersistentStoreCoordinator:psc];
        [context setUndoManager:nil];
    }
    return self;
}

-(NSString *) itemArchivePath
{
    NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dir = [docDirectories objectAtIndex:0];
    return [dir stringByAppendingPathComponent:@"hsc1.data"];
}

在每个控制器中,我都会得到单例执行操作。我计划围绕回合操作定义一个委托,并在我的AppDelegate中实现它,因此我只在应用程序中获得一次服务,但现在不认为这很重要...。

最佳答案

您确定数据实际上已损坏吗?受管对象上下文是高效的,在调试器中发生错误是正常的。从文档:

“故障是Core Data用来减少您的
应用程序的内存使用情况...”

参见http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdFaultingUniquing.html

如果数据实际上丢失并且无法通过其他方法访问,请确保使用相同的托管对象上下文来访问数据。如果数据尚未提交到数据存储,则MOC之间不会“同步”。

10-08 07:43
查看更多