Ios 仅返回null的核心数据

Ios 仅返回null的核心数据,ios,core-data,Ios,Core Data,我有许多对象(NSManagedObject子类),它们“似乎”被正确创建和保存,但是在重新加载时,我只能从对象中的任何变量中获取null 保存的对象数量正确,但没有任何变量 我已经检查过代码好几次了,我不知道问题是在保存方面还是在加载方面 如果您有任何想法,我将不胜感激! 提前谢谢 //Create a route - (CNSRoute *) createRoute { CNSRoute *p = [NSEntityDescription insertNewObjectForEnt

我有许多对象(NSManagedObject子类),它们“似乎”被正确创建和保存,但是在重新加载时,我只能从对象中的任何变量中获取null

保存的对象数量正确,但没有任何变量

我已经检查过代码好几次了,我不知道问题是在保存方面还是在加载方面

如果您有任何想法,我将不胜感激! 提前谢谢

//Create a route
- (CNSRoute *) createRoute
{

    CNSRoute *p = [NSEntityDescription insertNewObjectForEntityForName:@"CNSRoute" inManagedObjectContext:context];

    [allRoutes addObject:p];
    //NSLog(@"Route: %@", p);
    return p;
}


//Load all routes from core data
- (void) loadAllRoutes
{
    NSLog(@"All Routes: %@", allRoutes);
    if (!allRoutes) {
        NSFetchRequest *request = [[NSFetchRequest alloc] init];

        NSEntityDescription *e = [[model entitiesByName] objectForKey:@"CNSRoute"];
        [request setEntity:e];

        NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
        [request setSortDescriptors:[NSArray arrayWithObject:sd]];

        NSError *error;
        NSArray *result = [context executeFetchRequest:request error:&error];
        if (!result) {
            [NSException raise:@"Fetch Failed" format:@"Reason: %@", [error localizedDescription]];
        }
        NSLog(@"LOAD RESULT: %@", result);

        for (CNSRoute *route in result) {
            NSLog(@"Loading Route... %@", [route name]);
        }

        allRoutes = [[NSMutableArray alloc] init];

        [allRoutes setArray:result];

        //NSLog(@"%@", allLocations);

    }
}

//Save context
- (BOOL)saveChanges
{
    NSError *err = nil;

    BOOL successful = [context save:&err];
    if (!successful) {
        NSLog(@"ERROR SAVING: %@", [err localizedDescription]);
    }

    return successful;
}

//Save when application enters background
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[CNSTravelController sharedStore] saveChanges];
}

是的,我的意思是当我完全关闭应用程序,但不要删除它。是的,应用程序进入后台时会保存上下文。是的,这是iOS,特别是iPhone。我是通过按模拟器上的home按钮来停止应用程序,而不是在xcode中停止应用程序。我已经有好几天这个问题了。我尝试过从XCode停止,从多任务栏上的“kill”按钮停止(在没有任何调试会话的情况下运行之后),每次CoreData返回时都会返回正确数量的实体,但每个属性的值都为null。(我对CoreData的理解肯定有严重的不准确之处,所以我也希望能有所启发。)您能包含一些托管对象子类的实现吗?您是否使用从数据模型自动生成的代码?如果您得到的是对象,但没有值,那么听起来您的访问器方法是错误的。您可以更具体地说,哪些变量?name是其中一个具有nil值的吗?你在何时何地设置它们,何时何地阅读它们?