I'm afraid this is a bit of an abstract question. I'm basically writing an iPhone game which revolves around a Pet, a bit like a tamagotchi for example. I've created a Pet object, which contains all the data relevant to the Pet's status. The rest of the program accesses the Pet's data and performs relevant actions.
The pet is saved to file using encodeWithCoder and initWithCoder methods. So whenever a method in the program needs to access any pet data, it creates a new instance of the pet by loading it from file. If any changes are made, they are done within methods of the Pet class. These methods always end by writing the Pet to file.
So as the program is running, it is constantly writing to file and reading from file, every time a change is made. If I then want to add a new variable to the Pet, say a BOOL variable called showReaction (to determine whether or not to show the Pet's reaction to a certain event say), I have to include this variable in both my encodeWithCoder and initWithCoder methods. This seems a bit cumbersome since these methods are consequently growing larger and larger. And constantly reading and writing to file seems inefficient.
I would have thought a better approach would be to have a global variable which represents the Pet. And that any method in the program can accesses this global variable in order to change it. The only time I write to file is when the player exits the game, and I only read from file once when the game loads.
My question is - I'm not exactly sure of the best approach to this. And whether or not this is good programming practice? Should I declare the global variable in my main ViewController and then refrain from releasing it during the program's running? Will I be able to alter variables in the Pet class and have this data retained during the running of the program. I.e. setting showReaction to TRUE in one method, reading it in another method and setting it to FALSE in another method...
Any advice would be much appreciated. I've been writing this program for a while now, and am wondering if I should make these changes now before I ingrain any bad practices further. But given the size of my code, I don't want to experiment too much without checking with the experts first!
Thanks,
Michael