iPhone Core Data不刷新表

发布于 2024-08-20 03:16:10 字数 1145 浏览 8 评论 0原文

我正在尝试使用核心数据编写一个应用程序,并且我已经能够成功读取和写入核心数据数据库。但是,如果我在一个视图控制器中写入数据库,则在关闭应用程序然后再次重新打开之前,其他视图控制器将看不到更改。这真是令人沮丧。我不完全确定如何使刷新 - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag 方法起作用。如何获取对我的托管对象的引用?

无论如何,这是我用来读回数据的代码。这是在 viewDidLoad 方法中。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
    //Handle the error
}

[self setNewsTitlesArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

[newsSourcesTableView reloadData];

提前感谢您的帮助!

I'm trying to write an application with Core Data, and I have been able to successfully read and write to the core data database. However, if I write to the database in one view controller, my other view controllers will not see the change until the app is closed then reopened again. This is really frustrating. I'm not entirely sure how to get the refresh - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag method to work. How do I get a reference to my managed object?

Anyways, here's the code I'm using to read the data back. This is in the viewDidLoad method.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
    //Handle the error
}

[self setNewsTitlesArray:mutableFetchResults];
[mutableFetchResults release];
[request release];

[newsSourcesTableView reloadData];

Thanks for help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-08-27 03:16:10

我不完全确定您打算做什么,据我所知,您正在一个视图控制器中更改托管对象上下文,并且您希望结果在其他视图控制器中可见,这是正确的吗?无论如何,解决方案是监听 NSManagedObjectContextDidSaveNotification (保存上下文时发送)并将您希望受到更改影响的视图控制器注册为观察者:

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:myController selector:@selector(updateTable:) name:NSManagedObjectContextDidSaveNotification object:controller.context];

myController 控制器中的 updateTable 选择器可能看起来像这样这个:

- (void)updateTable:(NSNotification *)saveNotification
{
if (fetchedResultsController == nil)
{
    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
    //Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }       
}
else
{
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    // Merging changes causes the fetched results controller to update its results
    [context mergeChangesFromContextDidSaveNotification:saveNotification];  
    [self.tableView reloadData];
}

希望有帮助。

-奥斯卡

I am not entirely sure what do you intend to do from what I understand you are changing your managed object context in one view controller and you want the result to be visible in other view controllers is this correct?. Anyway a solution for this is to listen for the NSManagedObjectContextDidSaveNotification (which is send when the context is saved) and register as a observer the view controller you want to be afected by the changes:

NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:myController selector:@selector(updateTable:) name:NSManagedObjectContextDidSaveNotification object:controller.context];

The updateTable in the myController Controller the selector could look something like this:

- (void)updateTable:(NSNotification *)saveNotification
{
if (fetchedResultsController == nil)
{
    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
    //Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }       
}
else
{
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    // Merging changes causes the fetched results controller to update its results
    [context mergeChangesFromContextDidSaveNotification:saveNotification];  
    [self.tableView reloadData];
}

Hope that helps.

-Oscar

何以畏孤独 2024-08-27 03:16:10

实际上,我发现我将表格设置为加载为我的视图。当我将表格放入另一个视图时,一切正常。

Actually, what I found was that I had the table set to load as my view. When I put the table into another view, everything worked fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文