开发者

Any tips or best practices for adding a new item to a history while maintaining a maximum total number of items?

开发者 https://www.devze.com 2022-12-09 13:51 出处:网络
I\'m working on some basic logging/history functionality for a Core Data iPhone app. I want to maintain a maximum number of history items.

I'm working on some basic logging/history functionality for a Core Data iPhone app. I want to maintain a maximum number of history items.

My gen开发者_JS百科eral plan is to ignore the maximum when adding a new item and enforce it whenever I need to fetch all the items anyway (e.g. for searching or browsing the history). Alternatively, I could do it when adding a new item: fetch the current items, add the new one, and delete the oldest one if we're at the maximum. The second way seems less efficient, since I would be fetching all the items when I otherwise wouldn't need to.

So, the questions:

  • Which way is better? Is there an even better way to do this that I'm not considering?
  • How many items would be a reasonable maximum? The history is used for text field autocompletion, so more items means better usability, unless the number of items is so huge that it's slowing stuff down.

Thanks!


Whichever method is easier to implement is the right one. You shouldn't bother with a more efficient/more complicated implementation unless it proves it's needed.

If these objects are in a to-many relationship of some kind, I'd use the relationship to manage the maximum number. (Override add<Whatever>Object: and delete the extraneous items then).

If you're just fetching them, then that's really your only opportunity to filter them out. If you're using an NSArrayController, you might be able to implement a subclass that detects when new objects are added and chops off the extra ones.


If the items are added manually by the user, then you can safely use the method of cleaning up later. With text data, a user won't enter more a few hundred items at most and text data takes up very little room. If the items are added by software, you have to check every so many entries or risk spill over.

You might not want to spend a lot of time on this. Autocomplete is not that big, usually just a few hundred entries. I would right it the simplest way, with clean up later, and then fiddle with it only if you hit a definite performance bottleneck.

Remember, premature optimization is the root of all programming evil. That and the dweebs in marketing.

0

精彩评论

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

关注公众号