-2

When we archive a object use archiveRootObject .

Is it a time-consuming operation for archiving and unarchiving?

[NSKeyedArchiver archiveRootObject:xxx toFile:filePath];

In a thread .

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    [NSKeyedArchiver archiveRootObject:xxx toFile: filePath]
});

When do we need to use threads to archive or unarchive ?

1 Answers1

3

archiveRootObject:toFile: does 2 separate things: encodeRootObject and writeToFile. Encoding is CPU- and memory-bound. writeToFile is IO-bound.

encodeRootObject could be slow if you have a lot of objects in the graph, or you have a lot of cycles. There are some "Performance Considerations" in the docs.

It is a common practice to move IO-bound operations (like writeToFile) onto a background thread, because it can often be the order of magnitude slower, and because there are no guarantees about it (it can slow down depending on various other IO activities).

In general you should use Xcode Instruments Profiler to measure your particular case in a typical workload scenario, and see if there are any problems with your strategy that need improvement. The measurements might surprise you, as performance bottlenecks could be found in some other code.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33