我正在使用iCloud
在多个设备之间进行sqlite数据库同步
我在我的应用程序中实现了this功能,用于sqlite同步。
但是其他设备上的数据会在5分钟后更新。 这是来自iCloud方面的问题,因为据我所知,我们无法手动更新iCloud。
我知道使用sqlite进行icloud同步是不正确的,但我有更多的100个表,我无法迁移到核心数据。
请在下面找到我的代码:
AppDelegate.m
[self checkForiCloudAccess];
[[sqlmessenger shared] fireQueryForCloudData];
检查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以获取iCloud数据
- (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];
}
使用元数据查询对象更新数据库
- (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);
}
}
}
}
我从各个类中调用loaddata
来更新数据库。
无论如何我可以提前/快速更新数据或我的代码有任何问题吗?
先谢谢