0

这里正在创建我的数据库的备份。但我不想继续创建备份。我需要检查最近创建的备份数据库的 NSModificationDate 属性,并且只有在数据库被修改时才必须创建新的备份。谁可以帮我这个事。

-(IBAction)createdb:(id)sender
{
DatabaseList = [[NSMutableArray alloc]init];

NSDate *currentDateTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMddyyyyHHmmss"];
NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];

dbNameString = [NSString stringWithFormat:@"UW_%@.db",dateInStringFormated];

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];
NSString *dbName = @"UnitWiseDB.db";
NSString *dbPath1 = [documentFolderPath stringByAppendingPathComponent:dbNameString];
NSString *backupDbPath = [documentFolderPath stringByAppendingPathComponent:dbName];

NSError *error = nil;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:backupDbPath error:&error];
NSLog(@"Persistent store size: %@ bytes", [fileAttributes objectForKey:NSFileSize]);
NSLog(@"Modification Date: %@ ",[fileAttributes objectForKey:NSFileModificationDate]);

if ( ![[NSFileManager defaultManager] fileExistsAtPath:dbPath1])
{
    [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbPath1 error:nil];
}
NSLog(@"DBPath.......%@",dbPath1);

NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager contentsOfDirectoryAtPath:documentFolderPath error:nil];

for (NSString *s in fileList)
{
    NSLog(@"Backup.....%@", s);
    [DatabaseList addObject:s];
}

[ListViewTableView reloadData];

}
4

1 回答 1

1

您可以使用 trojanfoe 描述的简单技巧,而不是检查修改。

当您进行修改时,我的意思是添加/删除/编辑数据库中的任何记录,设置BOOL标志 = YES 并存储NSUSERDEFAULTS,创建备份后,将标志设置为 NO。

于 2013-06-12T10:53:00.077 回答