【发布时间】:2013-02-28 00:36:36
【问题描述】:
尝试实现一个应用程序,该应用程序在连接到 Internet 时将存储在本地数据库中的离线数据发送到 Web 服务器。我使用下面显示的代码。到目前为止,我已经测试它工作正常,不确定它是否适用于大量记录。我想知道对这段代码的任何调整是否可以提高性能???
注意
- 我知道这对于离线同步来说是最糟糕的代码,所以尝试一下 更好地调整它。
-
它是单向同步,从应用程序到服务器。
-(void)FormatAnswersInJSON { DMInternetReachability *checkInternet = [[DMInternetReachability alloc] init]; if ([checkInternet isInternetReachable]) { if ([checkInternet isHostReachable:@"www.apple.com"]) {//Change to domain responseArray = [[NSMutableArray alloc] init]; dispatch_async(backgroundQueue, ^(void) { NSArray *auditIDArray = [[NSArray alloc] initWithArray: [self getUnuploadedIDs]]; for (int temp = 0; temp < [auditIDArray count]; temp ++) { // Code to post JSON to server NSURLResponse *response; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (!error) { NSString *responseID = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; if ([responseID isEqualToString:@"ERROR"]) { //Error uploading records } else { [responseArray addObject:responseID]; } } else { //Error return; } } dispatch_async( backgroundQueue, ^{ /* Based on return code update local DB */ for (int temp = 0; temp < [responseArray count]; temp ++) { [self updateRecordsForID:[auditIDArray objectAtIndex:temp] withID:[responseArray objectAtIndex:temp]]; } }); }); } } } - (void)upload { //Called when internet connection available if(backgroundQueue){ dispatch_suspend(backgroundQueue); dispatch_release(backgroundQueue); backgroundQueue = nil; } backgroundQueue = dispatch_queue_create("com.XXXX.TestApp.bgqueue", NULL); dispatch_async(backgroundQueue, ^(void) { [self FormatAnswersInJSON]; }); }
【问题讨论】:
-
如果您上传 x 个项目,则服务器会抛出错误,您将不会更新本地数据库。我做对了吗?也许从那个中打破而不是返回,所以你可以将你的本地数据库更新到成功的地方。
-
是的。你说对了。我会改变的!
-
@7usam true,除非在发生故障时回滚更改很重要; “全有或全无”的方法。
标签: ios objective-c sqlite synchronization