将导入的数据保存到Core Data - 无法回读

时间:2012-04-02 09:01:06

标签: iphone objective-c ios core-data

当我的应用程序第一次运行时,我使用一些简单的代码从源读取一些数据,然后将其保存到核心数据以便将来读回。这是我的applicationDidFinishLaunching方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    categories = [[NSMutableArray alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults objectForKey:@"dataImported"]) {
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithObjects:@"Food & Drink", @"Medical", @"Hotel", @"Travel", nil];

        for(int i = 0; i < [temp_Categories count]; i++){
            //Insert a new object of type ProductInfo into Core Data
            NSManagedObject *categoryInfo = [NSEntityDescription
                                              insertNewObjectForEntityForName:@"Category" 
                                              inManagedObjectContext:self.managedObjectContext];

            //Set category entities values
            [categoryInfo setValue:[temp_Categories objectAtIndex:i] forKey:@"categoryName"];

            [categories addObject:[temp_Categories objectAtIndex:i]];
        }

        [defaults setObject:@"OK" forKey:@"dataImported"];
        [defaults synchronize];

        NSLog(@"Imported");
    }
    else {
        //read from core data
        NSError *error;
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Category" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

        for (int i = 0; i < [temp_Categories count]; i++){
            NSString *category = [[temp_Categories objectAtIndex:i] objectForKey:@"categoryName"];
            [categories addObject:category];
            NSLog(@"Success");
        }

        NSLog(@"Read From Core");
    }

    return YES;
}

当我第一次运行此代码时,它可以正常工作,但是当我将来运行它时(当它从核心数据读取时)它不起作用,它不会从核心数据和temp_Categories中读取数组count = 0.谁能解释我做错了什么?我在使用另一个应用程序中的plist文件尝试做同样的事情时遇到了同样的麻烦。

谢谢,

杰克

编辑1 我正在使用iOS 5.

编辑2 数据模型屏幕截图:Data Model

3 个答案:

答案 0 :(得分:1)

试着给你打电话

NSError* error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Failed to save: %@, %@", error, [error userInfo]);
}

插入对象后......

答案 1 :(得分:0)

您应该调用save方法。要将更改保存到persistent store,您必须调用save方法。在此之前,这些更改只是暂时存在于您的scratch board/ ManagedObjectContext

在此行之后添加保存方法:

[categoryInfo setValue:[temp_Categories objectAtIndex:i] forKey:@"categoryName"];

保存方法:

 NSError *error;

if (![managedObjectContext save:&error]) 
{
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
} 

我也不知道这条线在其他部分是否正常工作..

 NSString *category = [[temp_Categories objectAtIndex:i] objectForKey:@"categoryName"];

请尝试使用此行..

    Category *currentCategory = (Category *)[temp_Categories objectAtIndex:i];
    [categories addObject:currentCategory.categoryName];

答案 2 :(得分:0)

我设法让它与它一起工作:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    categories = [[NSMutableArray alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults objectForKey:@"dataImported"]) {
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithObjects:@"Food & Drink", @"Medical", @"Hotel", @"Travel", nil];

        for(int i = 0; i < [temp_Categories count]; i++){
            //Insert a new object of type Category into Core Data
            NSManagedObject *categoryInfo = [NSEntityDescription
                                             insertNewObjectForEntityForName:@"Category" 
                                             inManagedObjectContext:self.managedObjectContext];

            //Set category entities values
            [categoryInfo setValue:[temp_Categories objectAtIndex:i] forKey:@"name"];

            NSError *error;
            if (![self.managedObjectContext save:&error]) {
                NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
            }

            NSLog(@"Added category to Core Data");

            //Add to global array
            [categories addObject:[temp_Categories objectAtIndex:i]];
        }

        [defaults setObject:@"OK" forKey:@"dataImported"];
        [defaults synchronize];

        NSLog(@"Imported");
    }
    else {
        //read from core data
        NSError *error;
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Category" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

        for (NSManagedObject *object in temp_Categories){
            [categories addObject: [object valueForKey:@"name"]];
        }

        NSLog(@"Read From Core");
    }

    return YES;
}

感谢您的帮助。