无法从Core Data文件中获取数据

时间:2014-10-08 12:03:53

标签: ios uitableview core-data

当我从核心数据中获取记录时,它返回空值...我不知道问题是什么......

当我登记NSLog时,它显示如下:

"<Ad: 0x979fb30> (entity: Ad; id: 0x9799470 <x-coredata://B3AA111F-8307-4A16-B898-
403A804DFDFB/Ad/p22> ; data: <fault>)",
"<Ad: 0x979fd70> (entity: Ad; id: 0x9799480 <x-coredata://B3AA111F-8307-4A16-B898-
403A804DFDFB/Ad/p23> ; data: <fault>)

这是我的代码。 我在称为dbmanager的单独类中返回了所有核心数据函数。

- (void)viewDidLoad
{
DBManager *manager=[[DBManager alloc]init];
self.fetchedRecordsArray = [manager fetchAllAds];
[self.shoppingtbl reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.fetchedRecordsArray count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath
{

static NSString *CellIdentifier = @"ShoppingCart";
ShoppingCart *cell = (ShoppingCart*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier 
forIndexPath:indexPath];
cell.backgroundColor=[UIColor clearColor];
ad=[self.fetchedRecordsArray objectAtIndex:indexPath.row]
cell.addesc.text = ad.data;
cell.adId.text=[NSString stringWithFormat:@"%lu", (long)[ad.adId integerValue]];
cell.adstatus.text=[NSString stringWithFormat:@"%@",ad.state];
return cell;
}

名为Ad的实体名称。它包含Adid,州,数据。

在表格视图中,它显示字符串的空值,0显示数字..

1 个答案:

答案 0 :(得分:0)

我建议您放弃fetchedRecordsArray并接受NSFetchedResultsController。它专门用于处理核心数据和表视图。

这几乎肯定会消除您的问题并导致更强大和可扩展的代码。您可以通过这种方式显示100.000s广告,没有任何问题。

还有一个提示,即您可能需要事先检查的内容:很可能您没有正确保存数据。看起来你在创建后用[managedObjectContext save:&error]保存了每条记录,但是在用状态数据填充它之后你可能没有正确地做到这一点。检查这个的方法是枚举你的数组元素和NSLog这些信息位以检查它们是否在那里。