将 NSArray 与 NSMutableArray 进行比较,将 delta 对象添加到 NSMutableArray

发布于 2024-08-24 12:23:33 字数 1207 浏览 3 评论 0原文

我有一个 NSMutableArray ,其中填充了字符串对象。为了简单起见,我们会说对象是一个人,每个人对象都包含有关该人的信息。

因此,我将有一个 NSMutableArray,其中填充有人员对象:

person.firstName
person.lastName
person.age
person.height

等等。

初始数据源来自 Web 服务器,并在我的应用程序加载并完成服务器初始化时填充。我的应用程序定期轮询服务器以获取最新的名称列表。

目前,我正在创建结果集的 NSArray,清空 NSMutableArray,然后用 NSArray 重新填充 NSMutableArray > 销毁 NSArray 对象之前的结果。

这在某些层面上对我来说似乎效率低下,并且还给我带来了丢失表行引用的问题,我可以解决这个问题,但这样做可能会为自己创造更多的工作。

效率低下似乎是我应该能够比较两个数组并最终得到过滤后的 NSArray 。然后我可以将过滤后的集合添加到 NSMutableArray 中。这意味着我可以简单地将新数据附加到 NSMutableArray 中,而不是扔掉所有内容并重新填充。

相反,我需要反向执行相同的过滤器,以查看是否有需要从 NSMutableArray 中删除的记录。

有没有什么方法可以更有效地做到这一点?我是否忽略了文档中某些地方提到更简单技术的内容?

当我清空 NSMutableArray 并重新填充时遇到问题,因为任何引用表都会丢失其选定的行状态。我可以跟踪它并重新选择它,但我的理论是,使用某种形式的比较、添加对象和删除对象,而不是在一个块中处理整个数组可能意味着我保留我的行引用(假设该项目不是当然删除了)。

任何建议或帮助非常感谢。

更新

对每个订单项进行比较的快速枚举是否会像我一样快?这似乎是一个昂贵的操作,但使用最后一个快速枚举代码可能会非常有效...

解决方案

我最终采用了 Abizem 的建议。在处理大量数据时,创建数组的可变副本和对象的副本似乎比使用 sbooth 的解决方案稍快一些。两者都工作得很好,我只是通过使用可变复制方法获得了更多优势。话虽这么说,它确实让我看到了 NSSet,这是我以前没有看过的。

感谢您的反馈。

I have an NSMutableArray that is populated with objects of strings. For simplicity sake we'll say that the objects are a person and each person object contains information about that person.

Thus I would have an NSMutableArray that is populated with person objects:

person.firstName
person.lastName
person.age
person.height

And so on.

The initial source of data comes from a web server and is populated when my application loads and completes it's initialization with the server. Periodically my application polls the server for the latest list of names.

Currently I am creating an NSArray of the result set, emptying the NSMutableArray and then re-populating the NSMutableArray with NSArray results before destroying the NSArray object.

This seems inefficient to me on a few levels and also presents me with a problem losing table row references which I can work around, but might be creating more work for myself in doing so.

The inefficiency seems to be that I should be able to compare the two arrays and end up with a filtered NSArray. I could then add the filtered set to the NSMutableArray. This would mean that I can simply append new data to the NSMutableArray instead of throwing everything out and re-populating.

Conversely I would need to do the same filter in reverse to see if there are records that need removing from the NSMutableArray.

Is there any method to do this in a more efficient manner? Have I overlooked something in the docs some place that refers to a simpler technique?

I have a problem when I empty the NSMutableArray and re-populate in that any referencing tables lose their selected row state. I can track it and re-select it, but my theory is that using some form of compare and adding objects and removing objects instead of dealing with the whole array in one block might mean I keep my row reference (assuming the item isn't deleted of course).

Any suggestions or help much appreciated.

Update

Would it be just as fast to do a fast enumeration over each comparing each line item as I go? It seems like an expensive operation, but with the last fast enumeration code it might be pretty efficient...

Solution

I ended up going with Abizem's suggestion. Creating the mutable copy of the array and a copy of the object appears to be the slightly faster approach than using sbooth's solution when dealing with large sets of data. Both worked great, I just got more of an edge by using the mutable copy approach. That being said, it did open my eyes up to NSSet where I hadn't looked before.

Thanks for the feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-08-31 12:23:33

您可以使用 NSSet 轻松完成此类操作(假设您的 person 对象是唯一的):

NSSet *existingItems = [NSSet setWithArray:existingItemArray];
NSSet *newItems = /* Get the new items from the server */

// Determine which items were removed
NSMutableSet *removedItems = [NSMutableSet setWithSet:existingItems];
[removedItems minusSet:newItems];

// Determine which items were added
NSMutableSet *addedItems = [NSMutableSet setWithSet:newItems];
[addedItems minusSet:existingItems];

// Modify the original array
[existingItemArray removeObjectsInArray:[removedItems allObjects]];
[existingItemArray addObjectsFromArray:[addedItems allObjects]];

如果性能不佳,我会感到惊讶,因为我确信实现已经过优化。

You can use NSSet to do this type of thing easily (assuming your person objects are unique):

NSSet *existingItems = [NSSet setWithArray:existingItemArray];
NSSet *newItems = /* Get the new items from the server */

// Determine which items were removed
NSMutableSet *removedItems = [NSMutableSet setWithSet:existingItems];
[removedItems minusSet:newItems];

// Determine which items were added
NSMutableSet *addedItems = [NSMutableSet setWithSet:newItems];
[addedItems minusSet:existingItems];

// Modify the original array
[existingItemArray removeObjectsInArray:[removedItems allObjects]];
[existingItemArray addObjectsFromArray:[addedItems allObjects]];

I'd be surprised if the performance isn't decent, as I'm sure the implementation is optimized.

妖妓 2024-08-31 12:23:33

两点。

  1. 新的 NSArray 包含您需要显示的所有数据。这就是为什么要在 NSMutableArray 中添加和删除以匹配新数组的原因。
  2. 您不想丢失表中行的选定状态。

这是我的建议

  1. 而不是清空 NSMutableArray 并用新数组重新填充它;为什么不创建 NSArray 的 mutableCopy 并将其设置为新的 NSMutableArray 呢?
  2. 而不是担心项目的顺序(以及所选的行号);如何创建所选对象的副本,并在按照步骤 1 中创建新的 NSMutableArray 后,在新数组中找到匹配的对象,并使用其新索引将其设置为表中的所选行。

Two points.

  1. The new NSArray contains all the data you need to show. Which is why you are adding and deleting from the NSMutableArray to match the new one.
  2. You don't want to lose the selected state of the rows in your table.

Here’re my suggestions

  1. Rather than emptying the NSMutableArray and repopulating it with the new Array; why not create a mutableCopy of the NSArray and set that as your new NSMutableArray?
  2. Rather than worry about the order of the items (and hence the selected row number); how about creating a copy of the object that is selected, and after creating your new NSMutableArray as in step 1, find the matching object in the new array and set that as the selected row in the table using its new index.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文