无法正确获取对象CoreData

时间:2015-03-04 09:19:43

标签: ios objective-c core-data

我正在练习使用CoreData来存储数据和获取数据。 但是我遇到了一些困扰我好几个小时的问题。 我存储了一组来自索引0 - 107的对象,总共是108.它完美地运行。

存储的一些数据的NSLog:

2015-03-04 16:39:29.825 Colo[32365:1884749] <NSManagedObject: 0x7f83d06a7240> (entity: Color; id: 0xd000000000300000 <x-coredata://3DC5B29F-DA7C-4D7C-9D59-738A28A957C1/Color/p12> ; data: {
  colorArray = nil;
  fifthColor = "#5A3431";
  firstColor = "#F2EDDA";
  fourthColor = "#A65F4B";
  hexString = nil;
  index = 106;
  secondColor = "#BEAC94";
  star = 12;
  thirdColor = "#DA896E";
  title = "wm102\n        \n        ";
})
2015-03-04 16:39:29.826 Colo[32365:1884749] 107
2015-03-04 16:39:29.826 Colo[32365:1884749] <NSManagedObject: 0x7f83d06a7330> (entity: Color; id: 0xd000000000980000 <x-coredata://3DC5B29F-DA7C-4D7C-9D59-738A28A957C1/Color/p38> ; data: {
  colorArray = nil;
  fifthColor = "#242613";
  firstColor = "#100C17";
  fourthColor = "#FFE8B4";
  hexString = nil;
  index = 107;
  secondColor = "#604325";
  star = 12;
  thirdColor = "#D0A17D";
  title = "Create Now 2014 \U4f1a\U5834\n        \n        ";
})

但是当我尝试获取我存储到CoreData的这些对象时。

- (void)fetchDataFromCoreData
{
  AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
  NSManagedObjectContext *context = [delegate managedObjectContext];

  NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Color"
                                                     inManagedObjectContext:context];
  NSFetchRequest *request = [[NSFetchRequest alloc] init];
  [request setEntity:entityDescription];

  NSError *error;

  NSArray *objects = [context executeFetchRequest:request error:&error];

  if (!objects){
    NSLog(@"There was an error.");
  }

  for (NSManagedObject *oneObject in objects){
    NSString *title       = [oneObject valueForKey:@"title"];
    NSString *star        = [oneObject valueForKey:@"star"];
    NSString *index       = [oneObject valueForKey:@"index"];
    NSString *firstColor  = [oneObject valueForKey:@"firstColor"];
    NSString *secondColor = [oneObject valueForKey:@"secondColor"];
    NSString *thirdColor  = [oneObject valueForKey:@"thirdColor"];
    NSString *fourthColor = [oneObject valueForKey:@"fourthColor"];
    NSString *fifthColor  = [oneObject valueForKey:@"fifthColor"];
    int i = [index intValue];
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
    ColorCell *cell = (ColorCell *)[self.tableView cellForRowAtIndexPath:path];

    UIColor *first  = [Parser translateStringToColor:firstColor];
    UIColor *second = [Parser translateStringToColor:secondColor];
    UIColor *third  = [Parser translateStringToColor:thirdColor];
    UIColor *fourth = [Parser translateStringToColor:fourthColor];
    UIColor *fifth  = [Parser translateStringToColor:fifthColor];

    cell.firstColor.backgroundColor  = first;
    cell.secondColor.backgroundColor = second;
    cell.thirdColor.backgroundColor  = third;
    cell.fourthColor.backgroundColor = fourth;
    cell.fifthColor.backgroundColor  = fifth;
  }
}

不幸的是,cell中的cellForRowAtIndexPath以奇怪的方式重复使用,某些单元格为空白,只显示一个单元格。我在

做了两个断点
     int i = [index intValue];
     NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];

我注意到索引是break,82应该是81的下一个。

任何想法都会受到欢迎。

enter image description here enter image description here

这是我的第一个CoreData实践。请帮助我变得更好。

编辑:子类NSManagedObject: .H:

@interface ColorManagerObject : NSManagedObject

@property (nonatomic) UIColor *firstColor;
@property (nonatomic) UIColor *secondColor;
@property (nonatomic) UIColor *thirdColor;
@property (nonatomic) UIColor *fourthColor;
@property (nonatomic) UIColor *fifthColor;

@end

的.m:

- (void)setFirstColor:(UIColor *)firstColor
{
  [self setValue:firstColor forKey:@"firstColor"];
}

- (UIColor *)firstColor
{
  NSString *string = [self valueForKey:@"firstColor"];
  return [Parser translateStringToColor:string];
}

1 个答案:

答案 0 :(得分:1)

  1. 您的fetchRequest没有sortDescriptors,因此不会按特定顺序返回对象。
  2. 这不是tableViews的工作方式。
  3. 首先解决第一个问题,只需添加一个NSSortDescriptor即可。

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    NSSortDescriptor *indexSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES];
    request.sortDescriptors = @[indexSortDescriptor];
    

    第二个问题解释起来比较复杂。 TableView使用dataSource模式。它向您的dataSource询问对象,您不会将对象发送到tableView。您应该将获取的结果存储在NSArray中,并将该数组用作您的dataSource。然后实现三个基本的UITableViewDataSource方法。

    e.g:

    @property (strong, nonatomic) NSArray *objects;
    
    - (void)fetchDataFromCoreData {
        // ...
        self.objects = [context executeFetchRequest:request error:&error];
        [self.tableView reloadData]; // reload tableView so it contains all the new objects
        // ...
    }
    
    #pragma mark - UITableViewDataSource
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self.objects count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
        NSManagedObject *object = self.objects[indexPath.row];
        cell.textLabel.text = [object valueForKey:@"title"];
        return cell;
    }
    

    一旦有效,您可能需要阅读NSFetchedResultsController,这基本上是针对由Core Data支持的UITableViews。它提供了一个很好的委托,当插入或从CoreData中删除对象时,它将插入和删除单元格。

    您可能还想查看NSManagedObject子类,因此您不必使用[object valueForKey:@"title"],而是可以使用object.title