只有一个值写入Core Data。为什么?

时间:2012-01-20 12:48:22

标签: iphone objective-c xcode json core-data

我是新手,我想存储和检索Core Data数据库中的数据。 我从php文件中获取所有数据,该文件与SQL数据库通信。这个php文件返回一个JSON对象,然后由我的应用程序解析,然后通过执行以下操作写入Core Data

AppDelegate.m

-(void)writeDataIntoCoreData
{
    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *articles = [NSEntityDescription
                                 insertNewObjectForEntityForName:@"Articles" 
                                 inManagedObjectContext:context];

    for(int i= 0; i<[json count];i++){
        NSDictionary *info = [json objectAtIndex:i];

        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];
        NSNumber * myNumber = [f numberFromString:[info objectForKey:@"userID"]];

        [articles setValue:myNumber forKey:@"articleID"];
        [articles setValue:[info objectForKey:@"username"] forKey:@"author"];
        [articles setValue:[info objectForKey:@"user_pic"] forKey:@"text"];
        NSLog(@"Name = %@",[info objectForKey:@"username"]);
        NSLog(@"Text = %@",[info objectForKey:@"user_pic"]);
    }
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }


}

MainViewController.m

在这里,我尝试通过迭代Core Data来检索fetchedObjects数据库中的所有数据。

-(IBAction)showCD:(id)sender
{

    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Articles" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *info in fetchedObjects) {
        NSLog(@"id: %@", [info valueForKey:@"articleID"]); 
        NSLog(@"Name: %@", [info valueForKey:@"author"]);
        NSLog(@"Text: %@", [info valueForKey:@"text"]);


    }    

}

然而,NSLog只显示一个对象,即第一个写入数据库的对象。有什么问题?

2 个答案:

答案 0 :(得分:2)

移动你的

NSManagedObject *articles = [NSEntityDescription
                             insertNewObjectForEntityForName:@"Articles" 
                             inManagedObjectContext:context];

进入你的for循环,

for(int i= 0; i<[json count];i++){
    NSManagedObject *articles = [NSEntityDescription
                             insertNewObjectForEntityForName:@"Articles" 
                             inManagedObjectContext:context];
    NSDictionary *info = [json objectAtIndex:i];

    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * myNumber = [f numberFromString:[info objectForKey:@"userID"]];

    [articles setValue:myNumber forKey:@"articleID"];
    [articles setValue:[info objectForKey:@"username"] forKey:@"author"];
    [articles setValue:[info objectForKey:@"user_pic"] forKey:@"text"];
    NSLog(@"Name = %@",[info objectForKey:@"username"]);
    NSLog(@"Text = %@",[info objectForKey:@"user_pic"]);
}

答案 1 :(得分:2)

将呼叫转移到 循环中的insertNewObjectForEntityForName:inManagedObjectContext: 。您现在的方式是创建一个托管对象,然后重复更改其属性。