gpt4 book ai didi

iphone - 数据未更新的 NSFetchedResultsController

转载 作者:行者123 更新时间:2023-11-29 13:27:23 24 4
gpt4 key购买 nike

我正在制作一个从服务器获取产品列表的应用程序。然后我将它们存储在核心数据数据库中,并使用 GMGridView 呈现它们并且数据源是 NSFetchedResultsController。当我更改服务器中的产品详细信息时,我希望我的 iOS 应用程序同步并进行必要的更改,因此我实现了 NSFetchedResultsControllerDelegate 方法。我应该如何正确更新我的 gridView?

- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_currentData removeObjectAtIndex:indexPath.row];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];

[_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

[_gmGridView reloadData];

//[self updatePageControl];
break;

case NSFetchedResultsChangeDelete:
[_currentData removeObjectAtIndex:indexPath.row];

[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
//[self updatePageControl];
[_gmGridView reloadInputViews];
[_gmGridView reloadData];

break;

case NSFetchedResultsChangeUpdate:
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

////////might be irrelevant, but just trying it out
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_currentData removeObjectAtIndex:indexPath.row];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];

[_gmGridView reloadObjectAtIndex:newIndexPath.row animated:YES];
[_gmGridView reloadObjectAtIndex:indexPath.row animated:YES];

////////


[_gmGridView reloadInputViews];
[_gmGridView reloadData];

break;

case NSFetchedResultsChangeMove:
[_currentData removeObjectAtIndex:indexPath.row];
[_currentData insertObject:anObject atIndex:newIndexPath.row];
[_gmGridView removeObjectAtIndex:indexPath.row animated:YES];
[_gmGridView insertObjectAtIndex:newIndexPath.row animated:YES];

[_gmGridView reloadInputViews];

[_gmGridView reloadData];

break;
}
}

一些细节:

_currentData = [[self.fetchedResultsController fetchedObjects]mutableCopy];
//I did this because previously I wasn't using a fetchedResultsController but a NSMutableArray instead. I know that it's inefficient (because I have 2 models) but this is the simplest implementation I want to do now.

我通过修改相同的 UIManagedDocument alloc,在不同的类中更改 CoreData,从相同的本地 URL 初始化。

但是,我遇到了 2 个重要问题:

  • 数据库中的项目已更新(例如,更改产品名称或价格)但更改未反射(reflect)在用户界面中,即我无法正确重新加载我的 GMGridViewCell。 (注意上面reload相关的代码)
  • 大多数时候,我在服务器中更新的产品在数据库中是重复的,尽管我有防止此类错误的机制。 (在创建产品之前,我首先使用唯一标识符搜索现有产品。如果有现有产品,我只需修改其详细信息)。这是代码:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Product"];        

request.predicate = [NSPredicate predicateWithFormat:@"product_id = %@", [imonggoInfo objectForKey:PRODUCT_ID]];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];

request.sortDescriptors = [NSArray

arrayWithObject:sortDescriptor]; NSError *error = nil;

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

if (!matches | ([matches count] > 1)){
//handle error
}else if ([matches count] == 0){
//make a new product
}else{
//return existing
item = [matches lastObject];
}

最佳答案

原来我必须听取 NSManagedObjectContextDidSaveNotification 并且我必须合并在后台线程和主线程中修改的上下文中的更改。我从这个 question 中选择的答案中得到了这个想法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextChanged:) name:NSManagedObjectContextDidSaveNotification object:nil];

//那么上下文的变化就得合并

- (void)contextDidSave:(NSNotification*)notification{
NSLog(@"contextDidSave Notification fired.");
SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
[self.itemDatabase.managedObjectContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:NO];
}

关于iphone - 数据未更新的 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12776371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com