0

I've seen some of the example of using NSCoder to archive the things in your application

@interface MyApplicationData : NSObject <NSCoding, NSCopying> {
    NSMutableArray* items;
    NSMutableArray* categories;
}

Please see this thread. How to use NSCoder

Why does this improve the performance of your app? isn't that doing more things in your application? Can anyone tell me why and when you want to use archive?

EDITED: Quoted from a book, iOS 5 programming pushing the limit- Under Method for Storing your Cache: "Caching or saving data can be done either by storing it as archives of your data models (using NSKeyedArchiver) or using a higher-level database like raw SQLite or using object serialization framework like Core Data..." "....NSKeyedArchiver is implemented by implementing the NSCoding protocol."

I thought NSCoding or NSCoder can be used for Caching and it will improve performance. So is it not good practice to use it for caching?

Thanks in advance.

Community
  • 1
  • 1
tipsywacky
  • 3,374
  • 5
  • 43
  • 75

3 Answers3

2

NSCoding has nothing to do with performance, it's a technique for serialising an object or object graph, so you can save it to persistent storage or send it over a network.

Nick Forge
  • 21,344
  • 7
  • 55
  • 78
1

WhyA archive:

I am working on a project that requires to remember user's accumulative actions. Those actions envolved in moving a number of custom objects on the screen. The custom objects' attributes included current position, color, neighbors, ect. So those objects were stored in an array. So NSCoding was used to serialize and archive the array. The next time I need to recall a user's last state, I just read from the archived file. Without this capability, I imagine I would need to create some type of DB with table and fields to individually store those attributes data and do the reverse when needed to recreate user's current state as in my example.

user523234
  • 14,323
  • 10
  • 62
  • 102
  • So after you archived the attributes, even when you quit/terminate the program, you will still be able to remember user's last state? If I guess it correctly, NSCoder is a good option to use when it comes to a small amount of data, otherwise you can use Core Data instead. When it comes to saving the files, you'll also use NSCoder, right? – tipsywacky Jul 26 '12 at 05:17
  • 1
    Yes to the first. Not really to the second. You might have misread my answer a little. Here is a quote directly from Apple's doc: "This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces)." And here is the link: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html – user523234 Jul 26 '12 at 22:24
1

Why does this improve the performance of your app?

well, you need some context -- serialization may or may not "improve performance". it could improve performance over alternative serialized representations. that may make a difference, or it may not be significant.

isn't that doing more things in your application?

well, if you have a complex structure represented in xml/plist as opposed to a serialized object, then no -- the object that knows how to encode/decode itself could be much more performant than xml or json or some other intermediate representation (for a number of reasons). it can also be easier to manage, and represent complex structures more easily (in comparison to intermediate serialization representations).

Can anyone tell me why and when you want to use archive?

typically when you want to save an object or object graph's state to disk, or for transfer to another machine.

imagine you have document data, and you want to restore that document when the document is reopened -- your program must create a representation which correctly reconstructs the object or object graph's state later on.

justin
  • 104,054
  • 14
  • 179
  • 226