Sqlite sync with iCloud not updating data

2019-08-10 13:48发布

问题:

I am using iCloud for sqlite database sync between multiple devices.
i have implemented this functionality in my app for sqlite sync.

But data is updating after 5 mins on other devices. is this problem from iCloud side because as far as i know we cannot manually update iCloud.

i know it is not correct to use sqlite for icloud sync but i have more 100 tables and i cannot migrate to core data.

Please find my code below:

 AppDelegate.m
        [self checkForiCloudAccess];
        [[sqlmessenger shared] fireQueryForCloudData];

Check for iCloud Access

-(void)checkForiCloudAccess{
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) {
    NSLog(@"iCloud access at %@", ubiq);
    isCloudAccees= TRUE;
    }
    else {

        isCloudAccees= FALSE;
    UIAlertView *alertb = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No iCloud acces" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertb show];
    }
}

sqlmessenger.m

Fire Query to get iCloud Data

- (void)fireQueryForCloudData{

    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
    [query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.xml'", NSMetadataItemFSNameKey];

    [query setPredicate:pred];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query];

    [query startQuery];

}

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *que = [[NSMetadataQuery alloc]init];
    que = [notification object];
    [que disableUpdates];
    [que stopQuery];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSMetadataQueryDidFinishGatheringNotification
                                                  object:que];

    self.metaQuery = que;
    que = nil;

    UIAlertView *alrView = [[UIAlertView alloc]initWithTitle:@"" message:@"Cloud updated" delegate:Nil cancelButtonTitle:@"OK" otherButtonTitles:Nil, nil];

    [alrView show];

    //TODO: Comment this stupid method
    //[self fetcLargeTableFromCloud];

}

Update database using Metadata query object

 - (void)loadData:(NSString *)tableName {
        _TblName = tableName;

            for (NSMetadataItem *item in [self.metaQuery results])
            {
                NSString *filename = [item valueForAttribute:NSMetadataItemDisplayNameKey];

                if([filename isEqualToString:tableName])
                {
                    NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];

                    //Conflict resolve

                    //[fileVersion replaceItemAtURL:url options:0 error:nil];


                    NSData *file = [NSData dataWithContentsOfURL:url];

                    [NSFileVersion removeOtherVersionsOfItemAtURL:url error:nil];


                    NSArray *conflicts = [NSFileVersion unresolvedConflictVersionsOfItemAtURL:url];

                    for (NSFileVersion *conflict in conflicts)
                    {
                        conflict.resolved = YES;

                        NSLog(@"conflict %@",conflict);
                    }


                    if (file) {
                        _arrCloudData = [[NSMutableArray alloc]init];
                        _tmpDict = [[NSMutableDictionary alloc]init];


                        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:file];
                        [parser setDelegate:self];
                        [parser parse];


                        if (_arrCloudData.count !=0) {


                    [self deleteTable:tableName];


                    _arrColumnList = [self getColumnsForTable:tableName];
                            //[_arrColumnList removeObjectAtIndex:3];

                           // [_arrColumnList removeObjectAtIndex:5];
                           // [_arrColumnList removeObjectAtIndex:8];

                    NSString *columnNames = [_arrColumnList componentsJoinedByString:@","];
                     /*
                    NSMutableArray *arrtmp = [NSMutableArray arrayWithArray:_arrColumnList];

                    for (int i = 0; i<[arrtmp count]; i++) {
                        [arrtmp replaceObjectAtIndex:i withObject:@"?"];
                    }*/
                   // NSString *blidString = [arrtmp componentsJoinedByString:@","];

                    //TODO: Uncomment this lines
                    //NSString *query = [NSString stringWithFormat:@"Insert into %@ (%@) values (%@)",tableName,columnNames,blidString];

                    //TODO: comment this line
                    NSString *query = [NSString stringWithFormat:@"Insert into %@ (%@) values",tableName,columnNames];


                           // NSDate *parseFinish = [NSDate date];
                        //[self insertRowIntblMealDetails:tableName :query];

                        int exCount = _arrCloudData.count/500;
                        for (int i=0; i<=exCount; i++) {
                            if ([tableName isEqualToString:@"tblRecipeSubtypes"]) {
                            [self insertRowIntblMealDetails:tableName :query:i];
                            }
                            else{
                                [self insertRowIntbl:tableName :query :i];
                            }


                        }
                        /*
                            for (int i=0; i<[_arrCloudData count]; i++) {

                                //Genralized method
                                //[self insertRowInTableName:tableName:i:query];
                                //
                                //[self insertRowIntblMealDetails:tableName :i :query];
                                [self insertRowIntblMealDetails:tableName :query];

                        }*/
                    sqlite3_close(database);
                    /*
                    NSDate *Complete = [NSDate date];


                    NSTimeInterval PraseComplete = [parseFinish timeIntervalSinceDate:start];
                    NSTimeInterval LoopTime = [Complete timeIntervalSinceDate:parseFinish];
                    NSTimeInterval totalTime = [Complete timeIntervalSinceDate:start];


                    NSLog(@"Parse Execution Time: %f", PraseComplete);
                    NSLog(@"Loop Execution Time: %f", LoopTime);
                    NSLog(@"Total Execution Time: %@ %f ",tableName, totalTime);
                     */
                }
                        else{
                            NSLog(@"Empty Array %@",tableName);
                        }
                    }
                    else{
                        NSLog(@"Blank File %@",tableName);
                    }
                }
            }
    }

i call loaddata from respective classes to update database. is there anyway with that i can update data early/fast or is there any problem in my code?

Thanks in Advance