尝试实现一个应用程序,该应用程序在连接到 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]; }); }