简体   繁体   English

是否可以在Core Data中自动删除未引用的对象?

[英]Is it possible to delete unreferenced objects automatically in Core Data?

My data model contains two entities: Author and Book with a one to many relationship (one author may write several books). 我的数据模型包含两个实体:作者和具有一对多关系的书(一个作者可能写几本书)。

Let's say that there are only two books and two authors in DB as follows: 假设数据库中只有两本书和两名作者,如下所示:

  • Book A is assigned to Author X 图书A已分配给作者X
  • Book B is assigned to Author Y 图书B已分配给作者Y

Assuming following change is applied: 假设进行了以下更改:

  • Book B is assigned to a new Author Z. 书B被分配给新的作者Z.

Result: 结果:

  • Author Y exists in DB but points to no book. 作者Y存在于DB中,但指向没有书。

My question: is it possible to configure the data model so objects like Author Y will be automatically deleted when they are not referenced by any book? 我的问题:是否可以配置数据模型,以便在没有任何书引用时会自动删除诸如Author Y之类的对象?

Check out "delete propagation". 查看“删除传播”。 It's there to solve exactly that problem. 它可以解决这个问题。

If that doesn't do exactly what you want / need: You can override - (void)prepareForDeletion on the Book entity and at that point check for any Authors that are registered with the context and have pending changes (since their inverse will have changed) and have no books: 如果这不能完全满足您的需求,则可以:在Book实体上覆盖- (void)prepareForDeletion ,然后检查在上下文中注册并有未决更改的所有Author(因为它们的相反值已更改) )并且没有书籍:

{
    // ...
    [[NSNotificationCenter defaultNotificationCenter] addObserver:self selector:@selector(deleteOrphanedAuthors:) name:NSManagedObjectContext object:moc];
    // ...
}

- (void)deleteOrphanedAuthors:(NSNotification *)note;
{
    NSManagedObjectContext *moc = [note object];
    NSManagedObjectModel *mom = [[moc persistentStoreCoordinator] managedObjectModel];
    NSEntityDescription *authorEntity = [[mom entitiesByName] objectForKey:@"Author"];
    for (NSManagedObject *author in [moc updatedObjects]) {
        if ([author entity] == authorEntity) {
            if (![author hasFaultForRelationshipNamed:@"books"] && ([[author books] count] == 0)) {
                [moc deleteObject:author];
            }
        }
    }
}

Note: You can not pass nil as the object (ie context) to observe, since frameworks you use, might have their own context, and you do not want to mess with them. 注意:不能nil作为要观察的对象(即上下文)传递,因为您使用的框架可能有自己的上下文,并且您不想弄乱它们。

Also, note how this code is careful not to touch the author object if it's a fault. 另外,请注意,如果出现错误,此代码应谨慎操作,不要碰到author对象。 If a book is deleted, Core Data will change the corresponding author objects' inverse relationships, hence fault in that relationship, such that it is no longer a fault. 如果删除了一本书,Core Data将更改相应作者对象的反向关系,从而导致该关系中的错误,从而不再是错误。 And the code will only operate on those objects. 代码只对这些对象进行操作。

You will need to determine "orphaned" books manually. 您将需要手动确定“孤立的”书。

When you update the Author relationship you could check the old Author 's books relationship to see if it still has any books. 当您更新作者关系时,您可以检查旧Author的书籍关系,看它是否还有任何书籍。

Alternatively you could use notifications to determine when the NSManagedObjectContext changes: NSManagedObjectContextObjectsDidChangeNotification . 或者,您可以使用通知来确定NSManagedObjectContext何时更改: NSManagedObjectContextObjectsDidChangeNotification If you register for this notification you can check for a number of changes to Author objects. 如果您注册此通知,则可以检查Author对象的许多更改。 Have a look at that specific notification in the docs . 查看docs中的特定通知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM