與iCloud同步數(shù)據(jù)DB

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

if ([strTitle isEqualToString:NSLocalizedString(@"Backup_data_Sync_to_iCloud_Drive", nil)]) {
        NSLog(@"同步到云盤");
        // TODO: 需要判斷是否有網(wǎng)絡(luò)

        // 詢問是否要全部刪除
        NSString *strMsg = [NSString stringWithFormat:NSLocalizedString(@"注意: 同步到iCloud操作, 會(huì)覆蓋已在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:@"同步出錯(cuò)"];
                }
                
            }];

        }];
        
        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同步到本地操作, 會(huì)覆蓋本地已有的數(shù)據(jù)!", 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:@"同步出錯(cuò)"];
                }
            }];

        }];
        
        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同步
//數(shù)據(jù)庫表格
#define LZSqliteName @"userData"
#define LZSqliteDataTableName @"newUserAccountData"
#define LZSqliteGroupTableName @"userAccountGroup"
#define LZSqliteDataPasswordKey @"passwordKey"
//數(shù)據(jù)庫數(shù)據(jù)有更新的通知key
#define LZSqliteValuesChangedKey @"sqliteValuesChanged"

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

Snip20221015_4.png

參考:makeProgress

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容