比较以前备份的NSFileModificationDate

时间:2013-06-12 09:22:33

标签: iphone ios backup nsfilemanager

这是创建我的数据库的备份。但我不想继续创建备份。我需要检查最近创建的备份数据库的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];

}

1 个答案:

答案 0 :(得分:1)

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

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