0

The copy method only makes a shallow copy, i.e. a new copy of the collection itself, not all the objects storing inside. The book I am studying now suggested to use archive to create deep copy, i.e. use NSKeyedArchiver to save the collection object to an NSData object, then load it back to another collection object using NSKeyedUnarchiver. This method works, but I am not sure how efficient it is to cope with large collection objects. Is there any other deep copying method which might works better?

Alright, this is what I want to know:

  1. Any method which involves less coding?
  2. Any method which is more CPU efficient?
  3. Any method which can achieve both? :P
God_of_Thunder
  • 753
  • 3
  • 20
  • 46
  • 4
    Please define "better" :) If "better" means "less work for you", then `NSKeyedArchiver` solution is unbeatable; if "better" means "less CPU cycles" or "less peak memory requirements", then you can almost certaonly do better. However, it will be a lot more expensive in terms of your coding effort (especially considering that the alternative does not require any effort) and in terms of maintenance effort, so you should consider undertaking the task only if your profiler tells you so. – Sergey Kalinichenko Jul 24 '12 at 14:55

2 Answers2

3

What @dasblinkenlight said...

NSKeyedArchiver is designed especially for large object graphs. It is of course possible to build something more efficient if you have highly specialized knowledge of your object graph, but that would almost always be a mistake unless you have a very special problem.

Your book is correct. Use archivers to create deep copies.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
1

I am not sure of the numbers, but the NSKeyedArchiver route is not going to be very computationally expensive. It looks like the long way round, but really is not much more inefficient than something custom made (like @Rob Napier said). It is, however a lot more reusable.

When still in doubt, an interesting experiment may be a deep copy of a large but simple data set, and time that against something custom made.

Carelinkz
  • 936
  • 8
  • 27