【问题标题】:returning null value while fetching record from core data从核心数据中获取记录时返回空值
【发布时间】:2014-10-09 09:47:51
【问题描述】:

朋友们,当我从核心数据中获取记录时,它显示为 null 。我不知道是什么问题。如果有人能解决我的问题,我会非常高兴。这是我的代码。

注意:我在名为“dbmanager”的单独文件中返回了所有核心数据函数

dbmanager.m

- (NSArray *)fetchAllAds {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Ad" 
inManagedObjectContext:dbManager.managedObjectContext]];
NSError *error = nil;
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
return results;
}

- (Ad*)addToFavourite:(NSNumber*)adId text:(NSString*)text type:(LinkupType)type {

Ad *item = (Ad*)[NSEntityDescription insertNewObjectForEntityForName:@"Ad" 
inManagedObjectContext:self.managedObjectContext]; 
item.adId = adId;
item.data =text;
item.state = [NSNumber numberWithInt:type];
[self save];
return item;  
}

- (void)save {
NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error7 %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}
}

MYviewcontroller.m

- (void)viewDidLoad
 {
self.fetchedRecordsArray = [[DBManager instance]fetchAllAds];
[[DBManager instance]managedObjectContext];
[self.shoppingtbl reloadData];
//NSLog(@"the fetched data is %@",[[DBManager instance]fetchAllAds]);
[super viewDidLoad];
}

- (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];
ad=[self.fetchedRecordsArray objectAtIndex:indexPath.row];
cell.addesc.text =[NSString stringWithFormat:@"%@",ad.data];
cell.adId.text=[NSString stringWithFormat:@"%lu", (long)[ad.adId integerValue]];
cell.adstatus.text=[NSString stringWithFormat:@"%@",ad.state];
return cell;

}

请尝试解决我的问题。当我签入 nslog 时,它显示如下:

     "<Ad: 0x9751570> (entity: Ad; id: 0x974c6b0 <x-coredata://B3AA111F-8307-4A16-B898-403A804DFDFB/Ad/p56> ; data: <fault>)",
"<Ad: 0x974e370> (entity: Ad; id: 0x9751af0 <x-coredata://B3AA111F-8307-4A16-B898-403A804DFDFB/Ad/p57> ; data: <fault>)"

note:  status section is showing properly!

【问题讨论】:

  • 您上面的 NSLog 显示您有两个对象。如果记录这些对象的属性会显示什么?

标签: ios uitableview core-data nsfetchedresultscontroller nsmanagedobjectcontext


【解决方案1】:

使用 NSFetchedResultsController 而不是使用单独的 fetchedRecordsArray 数组。通过代码保持 ManagedObjects 不是线程安全的。

【讨论】:

  • 当我像这样创建一个新的管理对象请求时- NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
  • 表格视图单元格如何实现?
  • 不,它不起作用..它仍然在 id 和 desc section.status 部分显示空值。
猜你喜欢
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多