【发布时间】:2010-09-12 16:59:51
【问题描述】:
我的更新逻辑有问题。我需要在包含特定更新数据的应用程序包中包含一个文件。当应用程序发现这个文件时,它会检查它是第一次安装(不需要更新)还是以前的安装(应用更新)。不管结果如何,更新文件都需要删除,所以再也找不到了,否则每次运行应用都会应用更新,很糟糕。
由于无法从 [[NSBundle mainBundle] 中删除任何内容,因此我需要找出最好的方法来执行此操作。如果我可以在应用程序的库路径中包含更新文件,那就简单多了。
我需要这个的原因是因为在第一次加载时,应用程序会创建一个用户数据文件并将其存储在库路径中。从那时起,应用程序将加载该用户文件。在这种情况下,创建的文件具有过时的数据。我创建了一个包含更新数据的新文件以应用于用户的主文件。
谁能帮我解决这个问题?这是我所拥有的:
if ([self checkUpdateFile] == YES) {
[self applyUpdate];
}
-(BOOL)checkUpdateFile {
NSString *updateFilePath = [[NSBundle mainBundle]pathForResource:@"updateData"
ofType:@"dat" inDirectory:@"update"];
BOOL updateFileExists = [[NSFileManager defaultManager]
fileExistsAtPath: updateFilePath];
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *programDataPath = [libraryPath stringByAppendingPathComponent:
@"programData.dat"];
BOOL programFileExists = [[NSFileManager defaultManager]fileExistsAtPath:
programDataPath];
if (programFileExists == YES && updateFileExists == YES) {
NSLog(@"Update File is present, and so is a data file, so this is a previous install");
return YES;
} else {
NSLog(@"This is not an upgradeable version.");
if (updateFileExists == YES) {
NSLog(@"The update file is here but this is the first install.");
[[NSFileManager defaultManager]removeItemAtPath: updateFilePath
error:NULL];
BOOL doesFileStillExist = [[NSFileManager defaultManager]
fileExistsAtPath:updateFilePath];
if (doesFileStillExist == YES) {
NSLog(@"File still exists");
} else {
NSLog(@"File was deleted.");
}
}
return NO;
}
}
-(void)applyUpdate {
NSLog(@"Applying Update");
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"updateData"
ofType:@"dat" inDirectory:@"update"];
NSData *programData = [[NSData alloc] initWithContentsOfFile:filePath];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc]
initForReadingWithData:programData];
NSMutableArray *characterList = [[decoder
decodeObjectForKey:@"characterList"]retain];
int i = 0;
for (Character *player in characterList) {
NSMutableArray *movesList = player.moves;
Character *existingCharacter = [self.dataController.characterList
objectAtIndex:i];
NSLog(@"Found Character: %@",existingCharacter.name);
existingCharacter.moves = movesList;
i++;
}
BOOL doesFileStillExist = [[NSFileManager defaultManager]
fileExistsAtPath:filePath];
if (doesFileStillExist == YES) {
NSLog(@"File still exists");
} else {
NSLog(@"File was deleted.");
}
[self writeDataToDisk];
[characterList release];
[decoder release];
[programData release];
}
【问题讨论】: