vodkhang
vodkhang

Reputation: 18741

Iphone : Application crash because of old encodeObject after updating

Currently, my Iphone application is not released yet. When I worked with the simulator/device and I modify my application to add more cache into the encodeWithCode: and initWithCoder: . The problem is that when the application is loaded, I tried to use some of the encoded object which is not existing before. For example:

In the previous application version (e.g 1.2), I have this encode:

- (void)encodeWithCoder:(NSCoder*)coder {
    [coder encodeObject:myArray forKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_KEY];
}

But with new version (e.g 1.3), I use this init:

- (id)initWithCoder:(NSCoder*)coder {
    if (self = [super initWithCoder:coder]) {
        myArray = [[coder decodeObjectForKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_KEY] retain];
        myArray2 = [[coder decodeObjectForKey:NCITEMTABLE_ARCHIVE_HOME_ITEMS_2_KEY] retain];
    }
    return self;
}

and then the application will crash because it cannot find myArray2.

In the simulator or testing, I can just delete the old version and install from fresh. However, I am afraid that when it is released, I cannot tell my user to delete the old app and install the new fresh one. Have anyone experienced about this problem?

Upvotes: 0

Views: 354

Answers (2)

superfell
superfell

Reputation: 19040

In your initWithCoder you should be able to just call containsValueForKey to see if the key exists before trying to call decodeObjectForKey

Upvotes: 1

vodkhang
vodkhang

Reputation: 18741

I tried to use try catch to get the exception. It may not be the best answer but it works now . The problem is that it may have low performance when it has to do try catch exception, which is not recommended by Apple

  if (archive) {
        @try {
            [self unarchiveInitializingWithData:archive];
        }
        @catch (NSException * e) {
            NCLog (@"Cannot unarchive");
            [self normalInitializing];
        }
    } else {
        NCLog (@"Normal init");
        // normal init
        [self normalInitializing];
    }  

Upvotes: 0

Related Questions