使用 NSSortDescriptor 按时间错误排序(带有核心数据)

Posted

技术标签:

【中文标题】使用 NSSortDescriptor 按时间错误排序(带有核心数据)【英文标题】:Sorting using NSSortDescriptor by time bug (with core data) 【发布时间】:2015-01-09 15:01:01 【问题描述】:

请在这里帮助我。我遇到了一些非常奇怪的错误,试图对我的字符串表视图对象进行排序。

行为很简单,我有:

CreateViewController - 我在这里创建字符串并将它们添加到核心数据中

StackTableViewController - 这是我创建的字符串的表格视图。它们应该按日期排序,第一个单元格是最旧的,第二个单元格是在他之后创建的,依此类推......

HomeViewController - 这里我有一个 UILabel,它一直保存表格视图的第一个对象,如果我在表格中删除它,我会使用委托方法更新这个标签,

原来如此。

问题:

如果表格视图中没有字符串,然后我在创建页面中添加了一个字符串,它会被很好地创建,但如果添加另一个字符串,则新的字符串位于列表顶部...但如果我终止该应用程序并再次打开它的顺序很好。

在我添加更多字符串后还有一些其他奇怪的行为......但问题是,每当我终止应用程序并再次打开它时,一切都完美无缺......:/

这是相关代码:

CreateViewController.m

- (id)init  
    self = [super initWithNibName:@"CreateViewController" bundle:nil];
    if (self) 
    
    return self;



- (void)save 

    if (self.myTextView.text.length == 0) 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hey!" message:@"You can't save a blank string" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
     

    [self insertTeget];

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

    [self.myTextView resignFirstResponder];


//this is where I add the string to the NSManagedObject object I have called 'Target' 
- (void)insertTeget 

    CoreDataStack *stack = [CoreDataStack defaultStack];
    Target *target = [NSEntityDescription insertNewObjectForEntityForName:@"Target" inManagedObjectContext:stack.managedObjectContext];
    if (self.myTextView.text != nil) 
        target.body = self.myTextView.text;
    

    [stack saveContext];

这是表格视图:

StackTableViewController.m

- (id)init 

    self = [super initWithNibName:@"StackTableViewController" bundle:nil];
    if (self) 

        [self fetchData];
    
    return self;


//currentTarget is a string I have in the .h file to pass the home view controller the first string of the table view  
- (void)fetchData 
    [self.fetchedResultController performFetch:nil];
    if (self.fetchedResultController.fetchedObjects.count > 0) 
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        Target *current = [self.fetchedResultController objectAtIndexPath:indexPath];
        self.currentTarget = current.body;
    else 
        self.currentTarget = nil;
    


- (void)viewDidLoad 

    [super viewDidLoad];
    [self fetchData];


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    // Return the number of sections.
    return self.fetchedResultController.sections.count;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultController sections][section];
    return [sectionInfo numberOfObjects];


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    return UITableViewCellEditingStyleDelete;


//here I'm updating the delegate when A cell was deleted so I can update the label in the home view controller
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];
    CoreDataStack *stack = [CoreDataStack defaultStack];
    [[stack managedObjectContext] deleteObject:target] ;
    [stack saveContext];

    if ([_delegate respondsToSelector:@selector(didDeleteObject)]) 
        [self fetchData];
        [_delegate didDeleteObject];
    


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

    static NSString *cellIdentifier = @"StackTableViewCell";

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];

    StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    

    cell.cellLabel.text = target.body;

    return cell;


// this is my fetch request where im setting up the sort descriptor 
- (NSFetchRequest *)targetsFetchRequest 

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Target"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    return fetchRequest;



- (NSFetchedResultsController *)fetchedResultController 

    if (_fetchedResultController != nil) 
        return _fetchedResultController;
    

    CoreDataStack *stack = [CoreDataStack defaultStack];

    NSFetchRequest *fetchRequest = [self targetsFetchRequest];

    _fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:stack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    _fetchedResultController.delegate = self;

    return _fetchedResultController;


这是我更新标签的主视图控制器:

- (id)init 
    self = [super initWithNibName:@"HomeViewController" bundle:nil];
    if (self) 
        stackTableViewController = [[StackTableViewController alloc] init];
        stackTableViewController.delegate = self;
    
    return self;



- (void)viewWillAppear:(BOOL)animated 
    [stackTableViewController fetchData];
    self.homeLabel.text = stackTableViewController.currentTarget;


- (void)viewDidLoad 

    [super viewDidLoad];
    self.homeLabel.text = stackTableViewController.currentTarget;


//this is the delegate method
- (void)didDeleteObject  
    self.homeLabel.text = stackTableViewController.currentTarget;


- (IBAction)goToStack:(id)sender 

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


- (IBAction)goToCreate:(id)sender 

    CreateViewController *createViewController = [[CreateViewController alloc]initWithNibName:@"CreateViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:createViewController];
    [self.navigationController presentViewController:navigationController animated:YES completion:nil]

拜托拜托,这让我发疯了,为什么只有在我终止应用程序后订单才能正常? 谢谢@@@!

【问题讨论】:

我会悬赏 0f 50p 的答案,这将在 2 天内为我解决问题 :) 什么是time,它在哪里设置? Time os 是我的 nsmanagedobject 类中名为 target 的属性 【参考方案1】:

我没有看到你在任何地方设置时间。当您创建对象时,它应该设置为[NSDate date],如果它充当创建日期。

【讨论】:

这么愚蠢的错误:/谢谢sssss!这让我发疯了哈哈 在创建视图控制器中我添加了 target.time = [NSDate date];在 insertTarget 方法和 BAM 中,它可以工作:)

以上是关于使用 NSSortDescriptor 按时间错误排序(带有核心数据)的主要内容,如果未能解决你的问题,请参考以下文章

CKQuery 的 distanceToLocation 上没有 NSSortDescriptor?

如何使用一个选择器创建一个按多个键排序的 NSSortDescriptor

使用 NSSortDescriptor、Core Data 和 NSFetchedResultsController 按日期和名称对 UITableView 进行排序

NSSortDescriptor 按核心数据对多关系中的项目数排序

NSSortDescriptor 错误:“[NSSortDescriptor 计数]:无法识别的选择器发送到实例”

可以将“self”与 NSSortDescriptor 一起使用来按对象本身而不是对象的属性进行排序? (核心数据/NSFetchedResultsController)