开发者

Preventing duplicate NSArrays (one sorted, one unsorted)

开发者 https://www.devze.com 2023-01-26 09:07 出处:网络
I have an NSArray of objects, it is unordered. It\'s updated from the internet so I can\'t order it once and guarantee it\'s order later.

I have an NSArray of objects, it is unordered. It's updated from the internet so I can't order it once and guarantee it's order later.

Each item corresponds to a table开发者_高级运维 view cell, but the user can reorder these, and there are 2 sections in the tableview, although to begin with all the cells are in one section.

So I could create a duplicate NSArray and have this ordered. And save it to the hard drive. But this seems quite a waste, and what do I do when a new object is added to/removed from the unordered NSArray by an update over the internet.

So in sum:

  • How do i have one unordered NSArray and one ordered without duplication (and waste of memory)?

  • And how do I deal with updates to the unordered array, when the user hasn't set a location for new objects/cells?


Your question is not very clear:

  • Arrays are an "ordered collection" by definition (apart from the odd languages with "associative arrays").
  • "Ordering" is not the same as "sorting". [1, 2, 3] is sorted; [1, 3, 2] is not sorted, but it is ordered (assuming the usual comparator).
  • "Preventing duplicate NSArrays" is an unhelpful subject.

So I'll have to guess at what you're trying to say:

  • You have a list of things that you download from the internet.
  • You occasionally update the list of things downloaded.
  • The user can reorder the list.
  • You want to be able to update the list of things but preserve the user's ordering.

Well, first you need some way of finding out which items in the two lists are "equivalent". For example, the first list is [Apple, Banana, Orange] and the user puts it in juice-preference order [Orange, Apple, Banana]. If the second list is [apple, banana, orange] (because you decided that everything should be lowercase, and things that needed to be capitalized could be done with -[NSString capitalizedString], or whatever), you need a way of determining that Apple = apple and constructing a new list [orange, apple, banana].

It's not clear why you think you have to save the original list — yes, it means that you can just say "Apple and apple are both at index 0, so they're the same", but this also means that you can never change the default order, and you can never remove an item (you can replace it with a placeholder, but meh).

There are two easy solutions:

  • Keep a list of indices (e.g. you'd store [2, 0, 1] because Orange is at index 2, etc.). This is a bit of a pain. If you want to use NSArray, the easiest way is to wrap things in NSNumber.
  • Don't care about the original ordering. Let's say you have a "user-ordered" list [Orange, Apple, Banana] and a new "server-ordered" list [apple, grape, orange]. Iterate over the user-ordered list, picking out "equivalent" items from the server-ordered list to get [orange, apple] and [grape]. Then do something sane with the "new" items, like sticking it on the end to get [orange, apple, grape]. (In this example, Banana has been removed; we handle this case by simply not including it in the new list.)


If they're both referencing the same objects (and not copies), then the extra memory will be very minimal (NSArray overhead and pointers), and probably not worth worrying about.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号