1

我有许多对象(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];
}
4

0 回答 0