ios - 核心数据更新所有 "index"属性 (iOS)

标签 ios core-data

我在实体“任务”中的核心数据中有一个名为“boardIndex”的属性(NSNumber)。每个 boardIndex 都是唯一的,这意味着 boardIndex 3 不能有两个任务。

当我更新 boardIndex 时,比如从 3 到 5,我需要通过递增或递减 1(或什么都不做)来更新核心数据中的所有其他实体“boardIndex”属性。例如,boardIndex 为 4 的任务现在为 3,boardIndex 为 5 的任务现在为 4,原本为 3 的 boardIndex 现在为 5;类似于 indexPath 在 tableView 中的工作方式。

执行此操作的最佳做​​法是什么?我阅读了“Core Spotlight”,但我不确定这是否是正确的方法。任何帮助都会很棒。

代码,如果有帮助:

//This only updates one task, how to update other tasks?
task?.boardIndex = childIndexPath?.row as NSNumber?
DataBaseManager.sharedInstance.save()

最佳答案

When I update a boardIndex, say from 3 to 5, I need to update all the other entities "boardIndex" property in Core Data by incrementing or decrementing by 1 (or doing nothing).

最好的方法是使用一种数据结构来为您维护任务的顺序。使用 Core Data 做到这一点的方法是使用有序的对多属性。

创建一个单独的实体来保存任务列表。例如,您可能有一个与 Task 具有一对多、有序关系的 Board 实体。该有序关系将由 NSOrderedSet 表示。您可以向其中添加任务的对象。

I read about "Core Spotlight" but I am not sure if this is the correct way to go.

这是一种完全不同的索引——Spotlight 允许用户搜索他们设备上的数据,而 Core Spotlight 允许您在搜索中包含您应用的数据。

关于ios - 核心数据更新所有 "index"属性 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310577/

相关文章:

android - 用于移动应用程序之间数据同步的 Web 服务

iphone - CoreData 中的重复名称?

ios - 理解在 swift 中向 NSFetchrequest 添加谓词

swift - 将 Double 从核心数据返回到文本字段

iphone - 反向关系警告 - 询问使用反向关系的实践

ios - Cocos2d 雪碧连续

iphone - Cocos2d-iPhone : Handle Button Press and Release

ios - 具有相同高度的 SwiftUI HStack 元素

ios - 用户界面没有更新,而某些功能需要时间才能执行?

ios - 向我的项目添加核心数据时无法添加 sql 文件