HI朋友,我从核心数据中获取记录。显示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!
答案 0 :(得分:0)
使用NSFetchedResultsController而不是将fetchedRecordsArray数组与单独的fetch一起使用。通过代码保持ManagedObjects不是线程安全的。