简体   繁体   English

将核心数据与 NSMutableArray 同步

[英]Syncing up Core Data with a NSMutableArray

I know this is probably a pretty basic question, but I'm working with Core Data and a UITableView.我知道这可能是一个非常基本的问题,但我正在使用 Core Data 和 UITableView。 I import all the data from Core Data to various mutable arrays.我将所有数据从 Core Data 导入到各种可变的 arrays。 When a user rearranges the items in the table, it's easy enough to swap the items in the mutable arrays I obtained from core data earlier, but is there an easy way to sync the new mutable array with the existing core data?当用户重新排列表中的项目时,很容易交换我之前从核心数据获得的可变 arrays 中的项目,但是有没有一种简单的方法可以将新的可变数组与现有的核心数据同步? Any help is appreciated!任何帮助表示赞赏!

Use a NSFetchedResultsController .使用NSFetchedResultsController It greatly simplifies using a table view and Core Data together.它极大地简化了表视图和 Core Data 的结合使用。 No need worrying about intermediary arrays.无需担心中介 arrays。

I think your question is actually how to persist the order of the data in your table view rows in Core Data.我认为您的问题实际上是如何在 Core Data 的表视图行中保留数据的顺序

The answer is you have to introduce a new integer / NSNumber attribute to keep track.答案是你必须引入一个新的 integer / NSNumber属性来跟踪。 I have done this in a simple app managing a to-do list.我在一个管理待办事项列表的简单应用程序中完成了这项工作。

Lion added support for ordered to-many relationships, which is much easier than storing the indices within the entities. Lion 添加了对有序对多关系的支持,这比在实体中存储索引要容易得多。 If you're targeting Lion you can mark the relationship Ordered and access it using a new class NSOrderedSet , which will persist its order, like an array.如果您的目标是 Lion,您可以标记关系Ordered并使用新的 class NSOrderedSet访问它,这将像数组一样保持其顺序。

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

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