将 NSArray 与 NSMutableArray 进行比较,将 delta 对象添加到 NSMutableArray
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 NSSet 轻松完成此类操作(假设您的 person 对象是唯一的):
如果性能不佳,我会感到惊讶,因为我确信实现已经过优化。
You can use NSSet to do this type of thing easily (assuming your person objects are unique):
I'd be surprised if the performance isn't decent, as I'm sure the implementation is optimized.
两点。
这是我的建议
Two points.
Here’re my suggestions
mutableCopy
of the NSArray and set that as your new NSMutableArray?