CoreData 将数据重新加载到 UITableView 问题

发布于 2024-10-19 11:06:18 字数 1595 浏览 4 评论 0原文

我再次与 CoreData 斗争,我需要你的帮助 :P

我正在编写一个 RSS 阅读器。 设计基于使用 libxml2 库的 Apple TopSongs 示例。

当我的应用程序启动时,我会检查上次更新是什么时候。如果超过 1 小时前,我会删除当前的持久存储并再次下载所有内容。

if ([[NSFileManager defaultManager] fileExistsAtPath:self.persistentStorePath]) {
        NSError *error = nil;
        BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:self.persistentStorePath error:&error];
        NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
    }

我可以看到本地sql文件被删除了。

现在我正在创建一些 NSOperations 以从后台的提要中下载信息。

到目前为止一切都很好。

与每个 RSS 提要一样,有一个更新按钮,用于检查更改并更新 coreData。 首先,我决定删除所有内容并重新下载所有内容(我知道效率不高,但 RSS 提要没有 GUID,我需要实现一个哈希函数来确定是否有任何更改)。

所以我所做的基本上是调用与上面相同的代码来删除商店.. 再次重新下载所有 RSS 提要(首次加载应用程序时的逻辑相同)

我正在重新加载表格,但我仍然可以在 tableView 上看到相同的数据,一切都保持不变。

我决定更进一步,删除商店而不下载内容。

if ([[NSFileManager defaultManager] fileExistsAtPath:app.persistentStorePath]) {
    NSError *error = nil;
    BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:app.persistentStorePath error:&error];
    NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
}

    [self.managedObjectContext save:&err];
    [self.fetchedResultsController performFetch:&error];
    [self.tableView reloadData]

我预计会看到空表,但数据仍然存在。

这怎么可能?我删除了商店,重新获取并重新加载数据。

我做错了什么?

I am fighting with CoreData again and i need your help :P

I am writing a RSS reader.
Design is based on Apple TopSongs example using libxml2 library.

When my application is up I am checking to see when was the last update. If it was more than 1 hour ago i remove the current Persistent store and download everything again.

if ([[NSFileManager defaultManager] fileExistsAtPath:self.persistentStorePath]) {
        NSError *error = nil;
        BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:self.persistentStorePath error:&error];
        NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
    }

I can see that the local sql file was deleted.

Now i am creating some NSOperations to download information from the feeds in the background.

So far everything works great.

Like every RSS feed, there is an update button which suppose to check for changes and update the coreData.
For starter i decided i will delete everything and re-download everything (not efficent i know, but the RSS feed does not have guid and i need to implement a hash function to figure if there were any changes).

So what i'm doing is basically calling the same code as above to delete the store..
re-downloading all RSS feeds again (same logic when the application was first loaded)

I am reloading the tables but i can still see the same data on the tableView, everything remained untouched.

I decided to go even further, and to delete the store without downloading content.

if ([[NSFileManager defaultManager] fileExistsAtPath:app.persistentStorePath]) {
    NSError *error = nil;
    BOOL oldStoreRemovalSuccess = [[NSFileManager defaultManager] removeItemAtPath:app.persistentStorePath error:&error];
    NSAssert3(oldStoreRemovalSuccess, @"Unhandled error adding persistent store in %s at line %d: %@", __FUNCTION__, __LINE__, [error localizedDescription]);
}

    [self.managedObjectContext save:&err];
    [self.fetchedResultsController performFetch:&error];
    [self.tableView reloadData]

I expected to see empty tables but the data was still there.

How is this possible ? i deleted the store, re-fetched and reloaded the data.

What am i doing wrong?

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

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

发布评论

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

评论(2

柠北森屋 2024-10-26 11:06:18

嘿,不要删除整个核心数据数据库...删除核心数据数据库中的每个条目,而不是整个数据库本身。您可以通过以下方式从 Core Data 数据库中删除对象:

[context deleteObject:aCoreDataObject];

哦,还有一件事...如果您更改 Core Data 数据库,这将不会反映在表视图中。如果您想查看更改,您需要调用例如 [tableView reloadData];

Hey, don't delete the whole Core Data database...delete every entry in the Core Data database and not the whole base itself. You can delete an object from Core Data database by:

[context deleteObject:aCoreDataObject];

Oh one more thing...if you change your Core Data database, that won't be reflected in the tableview. If you want to see changes you need to call for example [tableView reloadData];

温柔嚣张 2024-10-26 11:06:18

我认为你的问题可能出在线路上

[self.managementObjectContext 保存:&err];

而是遵循@Ladislav的建议:

    NSFetchRequest * allObjectsRequest = [[NSFetchRequest alloc] init];
    [allObjectsRequest setEntity:[NSEntityDescription entityForName:@"Object" inManagedObjectContext:context]];
    [allObjectsRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

    NSError * error = nil;
    NSArray * objects = [context executeFetchRequest:allObjectsRequest error:&error];
    [allObjectsRequest release];
    //error handling goes here
    for (NSManagedObject * object in objects) {
        [context deleteObject:object];
    }
    [context save:&error];

I think your problem might be with the line

[self.managedObjectContext save:&err];

Rather follow @Ladislav's suggestion:

    NSFetchRequest * allObjectsRequest = [[NSFetchRequest alloc] init];
    [allObjectsRequest setEntity:[NSEntityDescription entityForName:@"Object" inManagedObjectContext:context]];
    [allObjectsRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

    NSError * error = nil;
    NSArray * objects = [context executeFetchRequest:allObjectsRequest error:&error];
    [allObjectsRequest release];
    //error handling goes here
    for (NSManagedObject * object in objects) {
        [context deleteObject:object];
    }
    [context save:&error];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文