与iCloud同步数据DB

1. 同步sqlit db到iCloud或者从iCloud同步到本地。

if ([strTitle isEqualToString:NSLocalizedString(@"Backup_data_Sync_to_iCloud_Drive", nil)]) {
        NSLog(@"同步到云盘");
        // TODO: 需要判断是否有网络

        // 询问是否要全部删除
        NSString *strMsg = [NSString stringWithFormat:NSLocalizedString(@"注意: 同步到iCloud操作, 会覆盖已在iCloud的备份!", nil)];
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:strMsg message:nil preferredStyle:UIAlertControllerStyleAlert];

        __weak __typeof(self) weakSelf = self;
        UIAlertAction* btn1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"开始同步", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"开始同步 同步到云盘");
            if (![self DetermineICloudPermissions]) {
                return;
            }
            
            [SVProgressHUD show];

            NSString *path = [[DataBase sharedDataBase] getOrCreateDBPath];
            [LZiCloud uploadToiCloud:path resultBlock:^(NSError *error) {
                [SVProgressHUD dismiss];
                if (error == nil) {
                    [SVProgressHUD showInfoWithStatus:@"同步成功"];
                } else {
                    
                    [SVProgressHUD showErrorWithStatus:@"同步出错"];
                }
                
            }];

        }];
        
        UIAlertAction* btn2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Do_Cancel", nil) style:UIAlertActionStyleCancel handler:nil];

        [btn1 setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
        [btn2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];

        [alert addAction : btn1];
        
        [alert addAction : btn2];
        
        [[CommonTool jsd_findVisibleViewController]  presentViewController:alert animated:YES  completion:nil];
        
    }else if ([strTitle isEqualToString:NSLocalizedString(@"Sync_data_from_iCloud_Drive_to_local", nil)]) {
        NSLog(@"云盘同步到本地");

        // 询问
        NSString *strMsg = [NSString stringWithFormat:NSLocalizedString(@"注意: 从iCloud同步到本地操作, 会覆盖本地已有的数据!", nil)];
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:strMsg message:nil preferredStyle:UIAlertControllerStyleAlert];

        __weak __typeof(self) weakSelf = self;
        UIAlertAction* btn1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"开始同步", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"开始同步 云盘同步到本地");
            if (![self DetermineICloudPermissions]) {
                return;
            }
            
            [SVProgressHUD show];
            [LZiCloud downloadFromiCloudWithBlock:^(id obj) {
                
                if (obj != nil) {
                    
                    NSData *data = (NSData *)obj;
                    
//                    NSString *strDataBasePath = [LZSqliteTool LZCreateSqliteWithName:LZSqliteName];
                    NSString *strDataBasePath = [[DataBase sharedDataBase] getOrCreateDBPath];
                    [data writeToFile:strDataBasePath atomically:YES];
                    [SVProgressHUD dismiss];
                    [SVProgressHUD showInfoWithStatus:@"同步成功"];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"Reset_APP_RootViewController_Notify" object:@"needJumpIndex1"];
                } else {
                    [SVProgressHUD dismiss];
                    [SVProgressHUD showErrorWithStatus:@"同步出错"];
                }
            }];

        }];
        
        UIAlertAction* btn2 = [UIAlertAction actionWithTitle:NSLocalizedString(@"Do_Cancel", nil) style:UIAlertActionStyleCancel handler:nil];

        [btn1 setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
        [btn2 setValue:[UIColor blackColor] forKey:@"_titleTextColor"];

        [alert addAction : btn1];
        
        [alert addAction : btn2];
        
        [[CommonTool jsd_findVisibleViewController]  presentViewController:alert animated:YES  completion:nil];

    }

2. 修改DB文件

- (NSString*)getOrCreateDBPath;

- (NSString*)getOrCreateDBPath {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [path objectAtIndex:0];
    NSString *dstPath = [docDirectory stringByAppendingPathComponent:@"MakeALittleProgressEveryDay.db"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:dstPath])
    {
        NSLog(@"文件不存在");
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MakeALittleProgressEveryDay" ofType:@"db"];
        NSLog(@"filePath: %@",filePath);
        NSLog(@"dstPath: %@",dstPath);
        [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:dstPath error:nil];
    }else{
        NSLog(@"文件存在 %@",dstPath);
    }
    return dstPath;
}

3. 修改PCH文件

// iCloud同步
//数据库表格
#define LZSqliteName @"userData"
#define LZSqliteDataTableName @"newUserAccountData"
#define LZSqliteGroupTableName @"userAccountGroup"
#define LZSqliteDataPasswordKey @"passwordKey"
//数据库数据有更新的通知key
#define LZSqliteValuesChangedKey @"sqliteValuesChanged"

4. 把LZICloudTools目录文件,添加到工程中

Snip20221015_4.png

参考:makeProgress

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容