Reputation: 4962
I have a property (NSNumber) in core data called "boardIndex" in the entity "Task". Each boardIndex is unique, meaning there cannot be two tasks with boardIndex 3.
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). For instance, the task whose boardIndex was 4 is now 3, the task whose boardIndex was 5 is now 4, and the boardIndex who was 3 is now 5; similar to how an indexPath works in a tableView.
What is the best practice for doing this? I read about "Core Spotlight" but I am not sure if this is the correct way to go. Any help would be great.
Code if it helps:
//This only updates one task, how to update other tasks?
task?.boardIndex = childIndexPath?.row as NSNumber?
DataBaseManager.sharedInstance.save()
Upvotes: 0
Views: 56
Reputation: 125027
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).
The best way to do that would be to use a data structure that maintains the order of the tasks for you. The way to do that with Core Data is to use an ordered to-many property.
Create a separate entity to hold the list of tasks. For example, you might have a Board
entity that has a to-many, ordered relationship to Task
. That ordered relationship will be represented by an NSOrderedSet
object to which you can add your tasks.
I read about "Core Spotlight" but I am not sure if this is the correct way to go.
That's a completely different kind of indexing -- Spotlight lets the user search through the data on their device, and Core Spotlight lets you include your app's data in the search.
Upvotes: 1