ios5核心数据:nsfetchresultcontroller刷新uitable
我正在开发一个带有故事板核心数据的应用程序。该应用程序将 uitabbarcontroller 作为根视图。我已经创建了实体并生成了类。每个选项卡都有自己的 unavigation 控制器。选项卡 1 中的视图仅将 uilabels 中的一些数据保存到数据库中。它工作正常并且数据在数据库中。 选项卡 2 中的视图在 uitableview 中显示数据库中的数据。仅当我终止应用程序并重新启动它时才会显示数据。所以 ui 表不会刷新。 第一种方法:我已将托管对象上下文从应用程序委托传递到两个视图。所以 ui 表不会刷新,直到杀死并重新启动。 第二种方法:我(错误)使用了应用程序委托,但结果仍然相同。
MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];
如何才能实现一个视图仅将数据添加到核心数据(现在正在这样做),而第二个视图收到更改通知并将其显示在 uitableview 中?
编辑
-(NSFetchedResultsController *) fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favis" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"chapterid" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[NSFetchedResultsController deleteCacheWithName:@"Master"];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
它是使用核心数据模板时的代码。仅进行调整以与我的应用程序配合使用。我的两个视图控制器都有它。
编辑2
我已经实现了 nsfetchedresultcontroller 在我的 uitableview 控制器中。 托管对象返回数据库中数据的确切数量,但 ui 表不会刷新。我也做了 [self.tableview reloaddata]
但没有运气
i'm working on an app with core data with storyboard. the app has uitabbarcontroller as rootview. i have created entity and generated the classes. each tab has it own uinavigation controller. the view in the tab 1 just saves some data in the database from uilabels. and it works fine and data is in the database.
the view in tab 2 displays the data from the database in uitableview. the data is only shown when i kill the app and restart it. so the ui table doesnt get refreshed.
first method: i have passed the managedobject context from the app delegate to the both views. so ui table doesnt get refreshed till kill and restart.
second method: i (mis)used the app delegate, but still the same result.
MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];
how can one achieve that one view only adds data to core data(which it does right now) and the second view get notified of changes and display it in uitableview?
edit
-(NSFetchedResultsController *) fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Favis" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"chapterid" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[NSFetchedResultsController deleteCacheWithName:@"Master"];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
it is the code when you use the core data template. only tweaked to work with my app. i have it in my both viewcontroller.
edit 2
i have implemented nsfetchedresultcontroller in my uitableview controller.
the manged object returns the exact number of data in the database, but ui table doesnt get refreshed. i also did [self.tableview reloaddata]
but no luck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在带有 UITableVIew 的 viewController 中实现 NSFetchedResultsControllerDelegate。该文档包含这些方法的完整实现。
然后让你的 viewController 成为 NSFetchedResultsController 的委托
fetchedResultsController.delegate = self;
In the viewController with the UITableVIew implement the methods for the NSFetchedResultsControllerDelegate. The documentation has the full implementation of those methods.
And then make your viewController the delegate of the NSFetchedResultsController
fetchedResultsController.delegate = self;
2 选项卡中应该有一些内容,以便在数据库中的数据更改时通知更新新数据,有吗?如果
NSManagedObjectContext, NSFetchedResultsController
在 2 选项卡中说实现 NSFetchedResultsController 委托方法。
在 appdelegate 中
现在肯定可以工作了
There should be some thing in 2 tab as to notify as data changed in database update the new data, Is there any? If
NSManagedObjectContext, NSFetchedResultsController
in 2 tab by sayingImplement
NSFetchedResultsController
delegation methods.in appdelegate
Surely it works now