核心数据,CloudKit-删除重复功能删除超出预期的
我为CloudKit设置了一个小测试应用程序,并在核心数据中使用预加载对象设置了一个。 当应用程序安装在多个设备上时,CloudKit由于预紧力而收集重复的数据。 我编写了一些重复的检查功能以删除重复和重新链接关系。
我的测试应用程序hast三个实体。 book
,<代码>作者,<代码> pub (发布者)。
- 一本书与许多作者和一位出版商有关系。
- 出版商和作者与许多书籍有关系。
我的副本检查实际上是在函数执行后仅有一位剩下的作者(第一位)的事实之外工作。 这可能很明显,但是我现在挠头了一段时间..
有人看到可能引起的东西吗?
这是一个比较屏幕截图:(数据是虚构的,我知道这些不是书籍:D)
这些是我检测到的重复数据相应执行的三个函数。
private func remove(duplicatedBooks: [Book], winner: Book, performingContext: NSManagedObjectContext) {
for book in duplicatedBooks {
// Update Authors
if let authors = book.authors {
for case let author as Author in authors {
if let authorsBooks = author.books as? NSMutableSet {
if authorsBooks.contains(book) {
authorsBooks.remove(book)
authorsBooks.add(winner)
}
}
}
}
// Update Publishers
if let publisherBooks = book.publisher?.books as? NSMutableSet {
if publisherBooks.contains(book) {
publisherBooks.remove(book)
publisherBooks.add(winner)
}
}
performingContext.delete(book)
}
}
private func remove(duplicatedAuthors: [Author], winner: Author, performingContext: NSManagedObjectContext) {
for author in duplicatedAuthors {
// Update Books
if let books = author.books {
for case let book as Book in books {
if let bookAuthors = book.authors as? NSMutableSet {
if bookAuthors.contains(author) {
bookAuthors.remove(author)
bookAuthors.add(winner)
}
}
}
}
performingContext.delete(author)
}
}
private func remove(duplicatedPublishers: [Pub], winner: Pub, performingContext: NSManagedObjectContext) {
for pub in duplicatedPublishers {
// Update Books
if let books = pub.books {
for case let book as Book in books {
book.publisher = winner
}
}
performingContext.delete(pub)
}
}
这是我如何进入删除功能的一个示例。 在这种情况下,重复书籍来自提取请求,该请求比较了书籍的名称,因此决定它们是重复的。
if var duplicateBooks = duplicateData as? [Book] {
let winner = duplicateBooks.first!
duplicateBooks.removeFirst()
remove(duplicatedBooks: duplicateBooks, winner: winner, performingContext: performingContext)
}
I setup a little test app for CloudKit with preloaded Objects in Core Data.
When the app gets installed on multiple devices, CloudKit collects duplicate data, due to the preload.
I wrote some duplicate check functions to delete duplicates and relink relationships.
My test app hast three Entities. Book
, Author
, Pub
(Publisher).
- A Book has a relationship to many Authors and to one Publisher.
- Publishers and Authors have relationships to many Books.
My duplicate checks actually work beside the fact that my Books have only one remaining Author (the first one) after the functions perform .
It's probably something very obvious but I'm scratching my head for a while now..
Does someone see something that could cause that?
Here is a comparison screenshot: (The data is fictive, I know that these are no books :D )
These are the three functions I perform accordingly to the duplicate data I detect.
private func remove(duplicatedBooks: [Book], winner: Book, performingContext: NSManagedObjectContext) {
for book in duplicatedBooks {
// Update Authors
if let authors = book.authors {
for case let author as Author in authors {
if let authorsBooks = author.books as? NSMutableSet {
if authorsBooks.contains(book) {
authorsBooks.remove(book)
authorsBooks.add(winner)
}
}
}
}
// Update Publishers
if let publisherBooks = book.publisher?.books as? NSMutableSet {
if publisherBooks.contains(book) {
publisherBooks.remove(book)
publisherBooks.add(winner)
}
}
performingContext.delete(book)
}
}
private func remove(duplicatedAuthors: [Author], winner: Author, performingContext: NSManagedObjectContext) {
for author in duplicatedAuthors {
// Update Books
if let books = author.books {
for case let book as Book in books {
if let bookAuthors = book.authors as? NSMutableSet {
if bookAuthors.contains(author) {
bookAuthors.remove(author)
bookAuthors.add(winner)
}
}
}
}
performingContext.delete(author)
}
}
private func remove(duplicatedPublishers: [Pub], winner: Pub, performingContext: NSManagedObjectContext) {
for pub in duplicatedPublishers {
// Update Books
if let books = pub.books {
for case let book as Book in books {
book.publisher = winner
}
}
performingContext.delete(pub)
}
}
That's an example of how I get to the remove functions.
The duplicateBooks in this case come from a fetch request that compared the names of the books and therefore decided that they are duplicates.
if var duplicateBooks = duplicateData as? [Book] {
let winner = duplicateBooks.first!
duplicateBooks.removeFirst()
remove(duplicatedBooks: duplicateBooks, winner: winner, performingContext: performingContext)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,就像我想的那样明显和愚蠢。
我没有删除副本并添加剩余的对象作为关系,而只将其删除并将其添加到集合中。
这是一个巧合,只有第一作者留下来。
而不是
}
应该是
Yep, it was as obvious and stupid as I thought.
Instead of removing the duplicate and adding the remaining object as a relationship, I only removed and added it to the set.
It was a coincidence that only the first author remained.
Instead of
}
It should have been