只有一个值被写入核心数据。为啥 ?
Posted
技术标签:
【中文标题】只有一个值被写入核心数据。为啥 ?【英文标题】:Only one value is being written into Core Data. Why ?只有一个值被写入核心数据。为什么 ? 【发布时间】:2012-01-20 12:48:21 【问题描述】:我是新手,我想从Core Data
数据库中存储和检索数据。
我从一个与 SQL 数据库通信的 php 文件中获取所有数据。这个 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
这里我尝试通过遍历fetchedObjects
来检索Core Data
数据库中的所有数据。
-(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 只显示一个对象,即第一个写入数据库的对象。怎么了?
【问题讨论】:
【参考方案1】:移动你的
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"]);
【讨论】:
啊是啊!非常感谢你 ! :)【参考方案2】:将调用移至循环中的insertNewObjectForEntityForName:inManagedObjectContext:
inside。您现在的做法是,您只创建 一个 托管对象,然后反复更改其属性。
【讨论】:
以上是关于只有一个值被写入核心数据。为啥 ?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我的 4 个 PHP 输入中只有 1 个正确写入数据库? [关闭]
为啥在刷新 Options 淘汰 observableArray 时我的 Select 值被取消选择(并且原始值仍然存在)?