删除coredata中的对象后根据通知重新加载tableview显示奇怪的结果

Posted

技术标签:

【中文标题】删除coredata中的对象后根据通知重新加载tableview显示奇怪的结果【英文标题】:reloading tableview as per notification after deletion of object in coredata shows weird result 【发布时间】:2013-05-12 15:41:57 【问题描述】:

我是这个核心数据的新手。只是为了熟悉关系,我创建了两个实体 CategoryList(由属性“categories”组成)和 ExpenseList(由属性“amountspent”组成),它们具有对多的关系。从“CategoryList”到“ExpenseList”的关系是“expenselist”。 第一个 tableview 显示每个类别的总费用,例如

food              -150  
transportation    -100  
mobile            -100  
shopping          -500    

第二个表格视图显示个人费用,如

food              -100  
food              -50  
transportation    -100  
mobile            -50  
mobile            -50  
shopping          -200  
shopping          -100  
shopping          -200  

我已经编写了代码以使用对象 ID 从我的第二个表视图中删除单个费用行,因为它是线程安全的。当我从我的第二个表格视图中删除一个食物条目时说

food              -50

当我导航回第一个表视图时,它应该更新食物并显示

food               -100

我已经使用默认通知中心注册了第一个表视图,并且每当删除对象时,第一个表视图正在发送通知(由 nslog 检查)。当第一个 tableview 收到通知时,它会重新加载 tableview 中的数据。但是,在重新加载 tableview 时,会显示从下一个 tableview 中删除其对象的类别的不需要的值。 两个表视图都通过导航控制器连接。

这是我的第一个 tableview 的 m.file。

#import "displayTable.h"
#import "CategoryList.h"
#import "ExpenseList.h"
#import "IncomeList.h"
#import "coreAppDelegate.h"
@interface displayTable ()

@end

@implementation displayTable
- (NSManagedObjectContext *)managedObjectContext

    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) 
        context = [delegate managedObjectContext];
    
    return context;

- (id)initWithStyle:(UITableViewStyle)style

   self = [super initWithStyle:style];
   if (self) 
    // Custom initialization
   
   return self;

- (void)viewDidLoad

   [super viewDidLoad];
   todaysL = [[todayslist alloc]init];
   self.expenseArray = [[NSMutableArray alloc] 
                       initWithObjects:@"0.00",
                                       @"0.00",
                                       @"0.00",
                                       @"0.00",
                                       @"0.00",
                                       @"0.00", 
                                       nil];
  self.categoryArray = [[NSMutableArray alloc] 
                    initWithObjects:@"food",
                                    @"transportation",
                                    @"fuel",
                                    @"mobile",
                                    @"shopping", 
                                    @"others", 
                                    nil];
  [self populateExpenses];  
  NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];   
  [[NSNotificationCenter defaultCenter]          
      addObserverForName:NSManagedObjectContextObjectsDidChangeNotification 
                  object:nil 
                   queue:[NSOperationQueue mainQueue] 
              usingBlock:^(NSNotification *notification)
              
                  NSLog(@"Notification received!");
                  [self populateExpenses];
                  //   [self viewDidLoad];
                  [self.tableView reloadData];
              ];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.

// self.navigationItem.rightBarButtonItem = self.editButtonItem;


- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];   

- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
  

#pragma mark-Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

   //#warning Potentially incomplete method implementation.
   // Return the number of sections.
   return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

   //#warning Incomplete method implementation.
   // Return the number of rows in the section.
   return [self.categoryArray count];

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

   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  // Configure the cell...

   if(cell == nil)
   
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                     reuseIdentifier:CellIdentifier];
   
   cell.textLabel.text = [_categoryArray objectAtIndex:indexPath.row];
   cell.detailTextLabel.text = [_expenseArray objectAtIndex:indexPath.row];
   return cell;

这是我获取数据的函数

-(void)populateExpenses


    NSManagedObjectContext *managedObjectContexts = [self managedObjectContext];
    NSEntityDescription *categorylistEntity = [NSEntityDescription entityForName:@"CategoryList" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:categorylistEntity];
    NSError *error =nil;
    NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:request error:&error];
    if(fetchedObjects == nil)
    
        NSLog(@"your fetching cause error");
    
    else
    
        for(CategoryList *categoryL in fetchedObjects)
        
            if([categoryL.categories isEqualToString: @"food"] )
            
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    food = food + expenseL.amountSpent;
                    //NSLog(@"display food %f",food);                       
                
                NSString *foodString = [NSString stringWithFormat:@"%.2f",food];
                [self.expenseArray replaceObjectAtIndex:0 withObject:foodString];
                //NSLog(@"foooood%f",food);
            
            else if ([categoryL.categories isEqualToString: @"transportation"])
            
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    transportation = transportation + expenseL.amountSpent;
                
                NSString *transportationString = [NSString stringWithFormat:@"%.2f",transportation];
                [self.expenseArray replaceObjectAtIndex:1 withObject:transportationString];
            
            else if ([categoryL.categories isEqualToString: @"fuel"])
            
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    fuel = fuel + expenseL.amountSpent;
                
                NSString *fuelString = [NSString stringWithFormat:@"%.2f",fuel];
                [self.expenseArray replaceObjectAtIndex:2 withObject:fuelString];
            
            else if([categoryL.categories isEqualToString: @"mobile"])
            
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    mobile = mobile + expenseL.amountSpent;
                
                NSString *mobileString = [NSString stringWithFormat:@"%.2f",mobile];
                [self.expenseArray replaceObjectAtIndex:3 withObject:mobileString];
            
            else if([categoryL.categories isEqualToString: @"shopping"])
            
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    shopping = shopping + expenseL.amountSpent;
                
                NSString *shoppingString = [NSString stringWithFormat:@"%.2f",shopping];
                [self.expenseArray replaceObjectAtIndex:4 withObject:shoppingString];
            
            else if ([categoryL.categories isEqualToString:@"others"])
                for(ExpenseList *expenseL in categoryL.expenselist)
                
                    others = others + expenseL.amountSpent;
                
                NSString *othersString = [NSString stringWithFormat:@"%.2f",others];
                [self.expenseArray replaceObjectAtIndex:5 withObject:othersString];
            NSLog(@"%@",othersString);
            
        //NSLog(@"count %d", self.expenseArray.count);
    
 
 /*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

// Return NO if you do not want the specified item to be editable.
return YES;

*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

if (editingStyle == UITableViewCellEditingStyleDelete) 
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
   
else if (editingStyle == UITableViewCellEditingStyleInsert) 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
   

*/

 /*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath



*/

/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

// Return NO if you do not want the item to be re-orderable.
    return YES;


*/

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

// Navigation logic may go here. Create and push another view controller.
/*
 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

 // ...

 // Pass the selected object to the new view controller.

 [self.navigationController pushViewController:detailViewController animated:YES];

 */

@end

但是当我退出并重新启动我的应用程序时,所有内容都会按预期更新,但在删除后不会立即更新。我不知道为什么。另外我不知道这是否是使用 coredata 做事的正确方法。如果有人可以指导我,提前谢谢

【问题讨论】:

【参考方案1】:

是的,您缺少核心数据的一些核心概念,这意味着您所做的工作比您需要的要多得多。

您有数据库中***类别的名称,但您不知道要按什么顺序获取它们,因此您创建了一个单独的数组并执行了一堆复杂的操作来获取值从数据库中以相同的顺序。将“displayOrder”字段添加到数据库并添加 order by 字段到 fetch 会更好。

完全摆脱您的 categoryArray 属性,在您的 populateExpenses 函数中,只需执行 fetch(使用 sort 子句)。将 fetchedObjects 保存到数组属性中。

您首先需要进行一次获取来获取您的***对象,但是一旦您拥有它们,您就很少需要进行更多次获取。

在你的 cellForRowAtIndexPath 中:

CategoryList* 类别 = [_fetchedObjects objecatAtIndex:indexPath.row]; cell.textLabel.text = category.categories; cell.detailTextLabel.text = [[category valueForKeyPath:@"expenselist.@sum.amountSpent"] 描述];

您不需要预取,您可以在显示数据时执行此操作。

更新对象以确保注意到您的更改:-

for ( NSManagedObject* object in self.categoryArray) 
    [self.managedObjectContext refreshObject: object mergeChanges:YES];

大多数情况下,它的作用是告诉对象它需要在下次请求时重新更新其值。

希望这会有所帮助。

【讨论】:

我尝试了该代码,当我运行时,我的应用程序崩溃并显示消息:此类不符合关键费用列表的关键值编码。' 好的,我明白了,我有点太笼统了,我会编辑答案 非常感谢,我会在尝试后告诉您【参考方案2】:

我通过在将浮点变量值存储在数组中后清除它来解决它。因为我之前没有清除浮点变量,所以在重新加载表格视图时,浮点变量中的先前值也会再次添加,从而给出不需要的值。 因此,在 viewWillAppear 方法中,我编写了重新加载表视图并清除了浮点变量内容,并且它起作用了。

【讨论】:

以上是关于删除coredata中的对象后根据通知重新加载tableview显示奇怪的结果的主要内容,如果未能解决你的问题,请参考以下文章

NSInternalInconsistencyException:使用 tableview CoreData 的无效更新

删除对象后重新加载 UICollectionViewController?

iPhone - 在删除 CoreData 中的对象后尝试保存新对象时应用程序崩溃

CoreData:错误:从上下文中删除托管对象后对其进行变异

Core Data + iCloud,更改通知插入和删除关系中的对象但不更新关系中现有实体的属性

核心数据重载数据库